From 92e73303851f1cf289bd8ed3dccc23d3885ce5f4 Mon Sep 17 00:00:00 2001 From: Carlos Rodrigues Date: Mon, 17 May 2021 19:04:31 +0100 Subject: [PATCH] types(props): Support `undefined` as default (#3783) --- packages/runtime-core/src/componentProps.ts | 2 +- test-dts/defineComponent.test-d.tsx | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/packages/runtime-core/src/componentProps.ts b/packages/runtime-core/src/componentProps.ts index a7235618..d86eb969 100644 --- a/packages/runtime-core/src/componentProps.ts +++ b/packages/runtime-core/src/componentProps.ts @@ -76,7 +76,7 @@ type RequiredKeys = { // don't mark Boolean props as undefined | BooleanConstructor | { type: BooleanConstructor } - ? K + ? T[K] extends { default: undefined | (() => undefined) } ? never : K : never }[keyof T] diff --git a/test-dts/defineComponent.test-d.tsx b/test-dts/defineComponent.test-d.tsx index ff04a0d6..d4c0b94c 100644 --- a/test-dts/defineComponent.test-d.tsx +++ b/test-dts/defineComponent.test-d.tsx @@ -23,6 +23,8 @@ describe('with object props', () => { h: boolean bb: string bbb: string + bbbb: string | undefined + bbbbb: string | undefined cc?: string[] | undefined dd: { n: 1 } ee?: () => string @@ -62,6 +64,14 @@ describe('with object props', () => { // annotation default: (props: any) => (props.bb as string) || 'foo' }, + bbbb: { + type: String, + default: undefined + }, + bbbbb: { + type: String, + default: () => undefined + }, // explicit type casting cc: Array as PropType, // required + type casting @@ -129,6 +139,8 @@ describe('with object props', () => { expectType(props.h) expectType(props.bb) expectType(props.bbb) + expectType(props.bbbb) + expectType(props.bbbbb) expectType(props.cc) expectType(props.dd) expectType(props.ee)