Skip to content

Commit 5db2b14

Browse files
authored
fix(types): add a type-only differentiator to assist Mixin's type infer (#3481)
fix #3468
1 parent c61e767 commit 5db2b14

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

packages/runtime-core/src/componentOptions.ts

+10
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,16 @@ interface LegacyOptions<
424424

425425
// runtime compile only
426426
delimiters?: [string, string]
427+
428+
/**
429+
* #3468
430+
*
431+
* type-only, used to assist Mixin's type inference,
432+
* typescript will try to simplify the inferred `Mixin` type,
433+
* with the `__differenciator`, typescript won't be able to combine different mixins,
434+
* because the `__differenciator` will be different
435+
*/
436+
__differentiator?: keyof D | keyof C | keyof M
427437
}
428438

429439
export type OptionTypesKeys = 'P' | 'B' | 'D' | 'C' | 'M' | 'Defaults'

test-dts/defineComponent.test-d.tsx

+35
Original file line numberDiff line numberDiff line change
@@ -775,6 +775,41 @@ describe('extends with mixins', () => {
775775
expectError(<MyComponent p2={'wrong type'} z={'z'} />)
776776
// @ts-expect-error
777777
expectError(<MyComponent mP1={3} />)
778+
779+
// #3468
780+
const CompWithD = defineComponent({
781+
data() {
782+
return { foo: 1 }
783+
}
784+
})
785+
const CompWithC = defineComponent({
786+
computed: {
787+
foo() {
788+
return 1
789+
}
790+
}
791+
})
792+
const CompWithM = defineComponent({ methods: { foo() {} } })
793+
const CompEmpty = defineComponent({})
794+
795+
defineComponent({
796+
mixins: [CompWithD, CompEmpty],
797+
mounted() {
798+
expectType<number>(this.foo)
799+
}
800+
})
801+
defineComponent({
802+
mixins: [CompWithC, CompEmpty],
803+
mounted() {
804+
expectType<number>(this.foo)
805+
}
806+
})
807+
defineComponent({
808+
mixins: [CompWithM, CompEmpty],
809+
mounted() {
810+
expectType<() => void>(this.foo)
811+
}
812+
})
778813
})
779814

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

0 commit comments

Comments
 (0)