Skip to content

Commit 955450f

Browse files
authored
types(runtime-core): improve the extracted instance types (#1936)
1 parent 475dd04 commit 955450f

File tree

2 files changed

+58
-2
lines changed

2 files changed

+58
-2
lines changed

packages/runtime-core/src/apiDefineComponent.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,8 @@ export function defineComponent<
189189
Extends,
190190
E,
191191
VNodeProps & AllowedComponentProps & ComponentCustomProps
192-
>
192+
> &
193+
Readonly<ExtractPropTypes<PropsOptions>>
193194
> &
194195
ComponentOptionsWithObjectProps<
195196
PropsOptions,

test-dts/defineComponent.test-d.tsx

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ describe('with object props', () => {
9494
// default + function
9595
ffff: {
9696
type: Function as PropType<(a: number, b: string) => { a: boolean }>,
97-
default: (a: number, b: string) => ({ a: true })
97+
default: (a: number, b: string) => ({ a: a > +b })
9898
},
9999
validated: {
100100
type: String,
@@ -799,3 +799,58 @@ describe('componentOptions setup should be `SetupContext`', () => {
799799
ctx: SetupContext
800800
) => any)
801801
})
802+
803+
describe('extract instance type', () => {
804+
const Base = defineComponent({
805+
props: {
806+
baseA: {
807+
type: Number,
808+
default: 1
809+
}
810+
}
811+
})
812+
const MixinA = defineComponent({
813+
props: {
814+
mA: {
815+
type: String,
816+
default: ''
817+
}
818+
}
819+
})
820+
const CompA = defineComponent({
821+
extends: Base,
822+
mixins: [MixinA],
823+
props: {
824+
a: {
825+
type: Boolean,
826+
default: false
827+
},
828+
b: {
829+
type: String,
830+
required: true
831+
},
832+
c: Number
833+
}
834+
})
835+
836+
const compA = {} as InstanceType<typeof CompA>
837+
838+
expectType<boolean>(compA.a)
839+
expectType<string>(compA.b)
840+
expectType<number | undefined>(compA.c)
841+
// mixins
842+
expectType<string>(compA.mA)
843+
// extends
844+
expectType<number>(compA.baseA)
845+
846+
// @ts-expect-error
847+
expectError((compA.a = true))
848+
// @ts-expect-error
849+
expectError((compA.b = 'foo'))
850+
// @ts-expect-error
851+
expectError((compA.c = 1))
852+
// @ts-expect-error
853+
expectError((compA.mA = 'foo'))
854+
// @ts-expect-error
855+
expectError((compA.baseA = 1))
856+
})

0 commit comments

Comments
 (0)