Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(types): add a type-only differentiator to assist Mixin's type infer #3481

Merged
merged 3 commits into from
Mar 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions packages/runtime-core/src/componentOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,16 @@ interface LegacyOptions<

// runtime compile only
delimiters?: [string, string]

/**
* #3468
*
* type-only, used to assist Mixin's type inference,
* typescript will try to simplify the inferred `Mixin` type,
* with the `__differenciator`, typescript won't be able to combine different mixins,
* because the `__differenciator` will be different
*/
__differentiator?: keyof D | keyof C | keyof M
}

export type OptionTypesKeys = 'P' | 'B' | 'D' | 'C' | 'M' | 'Defaults'
Expand Down
35 changes: 35 additions & 0 deletions test-dts/defineComponent.test-d.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -735,6 +735,41 @@ describe('extends with mixins', () => {
expectError(<MyComponent p2={'wrong type'} z={'z'} />)
// @ts-expect-error
expectError(<MyComponent mP1={3} />)

// #3468
const CompWithD = defineComponent({
data() {
return { foo: 1 }
}
})
const CompWithC = defineComponent({
computed: {
foo() {
return 1
}
}
})
const CompWithM = defineComponent({ methods: { foo() {} } })
const CompEmpty = defineComponent({})

defineComponent({
mixins: [CompWithD, CompEmpty],
mounted() {
expectType<number>(this.foo)
}
})
defineComponent({
mixins: [CompWithC, CompEmpty],
mounted() {
expectType<number>(this.foo)
}
})
defineComponent({
mixins: [CompWithM, CompEmpty],
mounted() {
expectType<() => void>(this.foo)
}
})
})

describe('compatibility w/ createApp', () => {
Expand Down