From 955450f53973e3226a4c9a1cd55a7d24e9ba93ca Mon Sep 17 00:00:00 2001 From: HcySunYang Date: Tue, 25 Aug 2020 09:37:22 +0800 Subject: [PATCH] types(runtime-core): improve the extracted instance types (#1936) --- .../runtime-core/src/apiDefineComponent.ts | 3 +- test-dts/defineComponent.test-d.tsx | 57 ++++++++++++++++++- 2 files changed, 58 insertions(+), 2 deletions(-) diff --git a/packages/runtime-core/src/apiDefineComponent.ts b/packages/runtime-core/src/apiDefineComponent.ts index 4a100d8f..eda62eaa 100644 --- a/packages/runtime-core/src/apiDefineComponent.ts +++ b/packages/runtime-core/src/apiDefineComponent.ts @@ -189,7 +189,8 @@ export function defineComponent< Extends, E, VNodeProps & AllowedComponentProps & ComponentCustomProps - > + > & + Readonly> > & ComponentOptionsWithObjectProps< PropsOptions, diff --git a/test-dts/defineComponent.test-d.tsx b/test-dts/defineComponent.test-d.tsx index 7a88c40b..cd08a428 100644 --- a/test-dts/defineComponent.test-d.tsx +++ b/test-dts/defineComponent.test-d.tsx @@ -94,7 +94,7 @@ describe('with object props', () => { // default + function ffff: { type: Function as PropType<(a: number, b: string) => { a: boolean }>, - default: (a: number, b: string) => ({ a: true }) + default: (a: number, b: string) => ({ a: a > +b }) }, validated: { type: String, @@ -799,3 +799,58 @@ describe('componentOptions setup should be `SetupContext`', () => { ctx: SetupContext ) => any) }) + +describe('extract instance type', () => { + const Base = defineComponent({ + props: { + baseA: { + type: Number, + default: 1 + } + } + }) + const MixinA = defineComponent({ + props: { + mA: { + type: String, + default: '' + } + } + }) + const CompA = defineComponent({ + extends: Base, + mixins: [MixinA], + props: { + a: { + type: Boolean, + default: false + }, + b: { + type: String, + required: true + }, + c: Number + } + }) + + const compA = {} as InstanceType + + expectType(compA.a) + expectType(compA.b) + expectType(compA.c) + // mixins + expectType(compA.mA) + // extends + expectType(compA.baseA) + + // @ts-expect-error + expectError((compA.a = true)) + // @ts-expect-error + expectError((compA.b = 'foo')) + // @ts-expect-error + expectError((compA.c = 1)) + // @ts-expect-error + expectError((compA.mA = 'foo')) + // @ts-expect-error + expectError((compA.baseA = 1)) +})