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

fix #3468
This commit is contained in:
HcySunYang 2021-03-30 06:07:36 +08:00 committed by GitHub
parent c61e767422
commit 5db2b141dc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 45 additions and 0 deletions

View File

@ -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'

View File

@ -775,6 +775,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', () => {