From 9d36c61796062644c63abad24a8eda85d6999991 Mon Sep 17 00:00:00 2001 From: HcySunYang Date: Tue, 18 Aug 2020 23:23:18 +0800 Subject: [PATCH] types(runtime-core): improve PropType inference precision (#1863) --- packages/runtime-core/src/componentProps.ts | 8 ++++---- test-dts/defineComponent.test-d.tsx | 14 ++++++++++++++ 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/packages/runtime-core/src/componentProps.ts b/packages/runtime-core/src/componentProps.ts index fcf707e0..9e6540cf 100644 --- a/packages/runtime-core/src/componentProps.ts +++ b/packages/runtime-core/src/componentProps.ts @@ -40,14 +40,14 @@ export type ComponentObjectPropsOptions

= { [K in keyof P]: Prop | null } -export type Prop = PropOptions | PropType +export type Prop = PropOptions | PropType type DefaultFactory = () => T | null | undefined -interface PropOptions { +interface PropOptions { type?: PropType | true | null required?: boolean - default?: T | DefaultFactory | null | undefined + default?: D | DefaultFactory | null | undefined validator?(value: unknown): boolean } @@ -83,7 +83,7 @@ type InferPropType = T extends null ? Record : T extends BooleanConstructor | { type: BooleanConstructor } ? boolean - : T extends Prop ? V : T + : T extends Prop ? (unknown extends V ? D : V) : T export type ExtractPropTypes< O, diff --git a/test-dts/defineComponent.test-d.tsx b/test-dts/defineComponent.test-d.tsx index b65ca88a..1dbb1929 100644 --- a/test-dts/defineComponent.test-d.tsx +++ b/test-dts/defineComponent.test-d.tsx @@ -27,6 +27,7 @@ describe('with object props', () => { eee: () => { a: string } fff: (a: number, b: string) => { a: boolean } hhh: boolean + ggg: 'foo' | 'bar' validated?: string } @@ -77,6 +78,11 @@ describe('with object props', () => { type: Boolean, required: true }, + // default + type casting + ggg: { + type: String as PropType<'foo' | 'bar'>, + default: 'foo' + }, validated: { type: String, validator: (val: unknown) => val !== '' @@ -97,6 +103,7 @@ describe('with object props', () => { expectType(props.eee) expectType(props.fff) expectType(props.hhh) + expectType(props.ggg) expectType(props.validated) // @ts-expect-error props should be readonly @@ -128,6 +135,7 @@ describe('with object props', () => { expectType(props.eee) expectType(props.fff) expectType(props.hhh) + expectType(props.ggg) // @ts-expect-error props should be readonly expectError((props.a = 1)) @@ -146,6 +154,7 @@ describe('with object props', () => { expectType(this.eee) expectType(this.fff) expectType(this.hhh) + expectType(this.ggg) // @ts-expect-error props on `this` should be readonly expectError((this.a = 1)) @@ -177,6 +186,7 @@ describe('with object props', () => { eee={() => ({ a: 'eee' })} fff={(a, b) => ({ a: a > +b })} hhh={false} + ggg="foo" // should allow class/style as attrs class="bar" style={{ color: 'red' }} @@ -194,6 +204,10 @@ describe('with object props', () => { // @ts-expect-error wrong prop types ) + expectError( + // @ts-expect-error wrong prop types + + ) // @ts-expect-error expectError() })