]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
types(props): Support `undefined` as default (#3783)
authorCarlos Rodrigues <david-181@hotmail.com>
Mon, 17 May 2021 18:04:31 +0000 (19:04 +0100)
committerGitHub <noreply@github.com>
Mon, 17 May 2021 18:04:31 +0000 (14:04 -0400)
packages/runtime-core/src/componentProps.ts
test-dts/defineComponent.test-d.tsx

index a72356184a0f54b34c93930d1aea5f533cef7ec1..d86eb9698947da8f21187ab4b907e19e4f07ecf5 100644 (file)
@@ -76,7 +76,7 @@ type RequiredKeys<T> = {
     // don't mark Boolean props as undefined
     | BooleanConstructor
     | { type: BooleanConstructor }
-    ? K
+    ? T[K] extends { default: undefined | (() => undefined) } ? never : K
     : never
 }[keyof T]
 
index ff04a0d6f3fd2c547fa79d5830b951f1b04ef2eb..d4c0b94c30ef3c7cda7af086f5d6b473862a1b44 100644 (file)
@@ -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<string[]>,
       // required + type casting
@@ -129,6 +139,8 @@ describe('with object props', () => {
       expectType<ExpectedProps['h']>(props.h)
       expectType<ExpectedProps['bb']>(props.bb)
       expectType<ExpectedProps['bbb']>(props.bbb)
+      expectType<ExpectedProps['bbbb']>(props.bbbb)
+      expectType<ExpectedProps['bbbbb']>(props.bbbbb)
       expectType<ExpectedProps['cc']>(props.cc)
       expectType<ExpectedProps['dd']>(props.dd)
       expectType<ExpectedProps['ee']>(props.ee)