From: Evan You Date: Tue, 18 Aug 2020 15:37:34 +0000 (-0400) Subject: types(runtime-core): argument for props validator X-Git-Tag: v3.0.0-rc.6~15 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9f92871c809b4d308de860dc0c04645090004d66;p=thirdparty%2Fvuejs%2Fcore.git types(runtime-core): argument for props validator Unfortunately it requires explicit annotation for now --- diff --git a/packages/runtime-core/src/componentProps.ts b/packages/runtime-core/src/componentProps.ts index 9e6540cfa1..b07ca81d87 100644 --- a/packages/runtime-core/src/componentProps.ts +++ b/packages/runtime-core/src/componentProps.ts @@ -42,7 +42,7 @@ export type ComponentObjectPropsOptions

= { export type Prop = PropOptions | PropType -type DefaultFactory = () => T | null | undefined +type DefaultFactory = (props: Data) => T | null | undefined interface PropOptions { type?: PropType | true | null diff --git a/test-dts/defineComponent.test-d.tsx b/test-dts/defineComponent.test-d.tsx index 41b26f266a..6918db61c8 100644 --- a/test-dts/defineComponent.test-d.tsx +++ b/test-dts/defineComponent.test-d.tsx @@ -18,6 +18,7 @@ describe('with object props', () => { b: string e?: Function bb: string + bbb: string cc?: string[] | undefined dd: { n: 1 } ee?: () => string @@ -46,6 +47,11 @@ describe('with object props', () => { bb: { default: 'hello' }, + bbb: { + // Note: default function value requires arrow syntax + explicit + // annotation + default: (props: any) => (props.bb as string) || 'foo' + }, // explicit type casting cc: Array as PropType, // required + type casting @@ -85,6 +91,7 @@ describe('with object props', () => { }, validated: { type: String, + // validator requires explicit annotation validator: (val: unknown) => val !== '' } }, @@ -94,6 +101,7 @@ describe('with object props', () => { expectType(props.b) expectType(props.e) expectType(props.bb) + expectType(props.bbb) expectType(props.cc) expectType(props.dd) expectType(props.ee)