From c2913d57d14449775faf1f2e5647e6d1f3d3f920 Mon Sep 17 00:00:00 2001 From: Carlos Rodrigues Date: Wed, 19 Aug 2020 21:36:42 +0100 Subject: [PATCH] fix(types): handling PropType with default value (#1896) fix #1891 --- packages/runtime-core/src/componentProps.ts | 2 +- test-dts/defineComponent.test-d.tsx | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/packages/runtime-core/src/componentProps.ts b/packages/runtime-core/src/componentProps.ts index 48636f65..90abf414 100644 --- a/packages/runtime-core/src/componentProps.ts +++ b/packages/runtime-core/src/componentProps.ts @@ -47,7 +47,7 @@ type DefaultFactory = (props: Data) => T | null | undefined interface PropOptions { type?: PropType | true | null required?: boolean - default?: D | DefaultFactory | null | undefined + default?: D | DefaultFactory | null | undefined | object validator?(value: unknown): boolean } diff --git a/test-dts/defineComponent.test-d.tsx b/test-dts/defineComponent.test-d.tsx index 00dbe3c1..7a88c40b 100644 --- a/test-dts/defineComponent.test-d.tsx +++ b/test-dts/defineComponent.test-d.tsx @@ -30,6 +30,7 @@ describe('with object props', () => { fff: (a: number, b: string) => { a: boolean } hhh: boolean ggg: 'foo' | 'bar' + ffff: (a: number, b: string) => { a: boolean } validated?: string } @@ -90,6 +91,11 @@ describe('with object props', () => { type: String as PropType<'foo' | 'bar'>, default: 'foo' }, + // default + function + ffff: { + type: Function as PropType<(a: number, b: string) => { a: boolean }>, + default: (a: number, b: string) => ({ a: true }) + }, validated: { type: String, // validator requires explicit annotation @@ -113,6 +119,7 @@ describe('with object props', () => { expectType(props.fff) expectType(props.hhh) expectType(props.ggg) + expectType(props.ffff) expectType(props.validated) // @ts-expect-error props should be readonly