From da0111806aa6e2cb076c79253990d79fb4f5777f Mon Sep 17 00:00:00 2001 From: tycho Date: Sun, 4 Aug 2024 08:19:24 +0800 Subject: [PATCH] fix(withDefaults/types): ensure default values of type `any` do not include `undefined` --- packages/dts-test/setupHelpers.test-d.ts | 6 +++++- packages/runtime-core/src/apiSetupHelpers.ts | 3 ++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/packages/dts-test/setupHelpers.test-d.ts b/packages/dts-test/setupHelpers.test-d.ts index 729f9b97d05..b134a4ca08e 100644 --- a/packages/dts-test/setupHelpers.test-d.ts +++ b/packages/dts-test/setupHelpers.test-d.ts @@ -42,7 +42,8 @@ describe('defineProps w/ generics', () => { test() }) -describe('defineProps w/ type declaration + withDefaults', () => { +describe('defineProps w/ type declaration + withDefaults', () => { const res = withDefaults( defineProps<{ number?: number @@ -55,6 +56,7 @@ describe('defineProps w/ type declaration + withDefaults', () => { z?: string bool?: boolean boolAndUndefined: boolean | undefined + foo?: T }>(), { number: 123, @@ -64,6 +66,7 @@ describe('defineProps w/ type declaration + withDefaults', () => { genStr: () => '', y: undefined, z: 'string', + foo: '' as any, }, ) @@ -80,6 +83,7 @@ describe('defineProps w/ type declaration + withDefaults', () => { expectType(res.x) expectType(res.y) expectType(res.z) + expectType(res.foo) expectType(res.bool) expectType(res.boolAndUndefined) diff --git a/packages/runtime-core/src/apiSetupHelpers.ts b/packages/runtime-core/src/apiSetupHelpers.ts index 39d8edbcc20..c233fd350cb 100644 --- a/packages/runtime-core/src/apiSetupHelpers.ts +++ b/packages/runtime-core/src/apiSetupHelpers.ts @@ -1,4 +1,5 @@ import { + type IfAny, type LooseRequired, type Prettify, type UnionToIntersection, @@ -305,7 +306,7 @@ type PropsWithDefaults< > = Readonly> & { readonly [K in keyof Defaults]-?: K extends keyof T ? Defaults[K] extends undefined - ? T[K] + ? IfAny, T[K]> : NotUndefined : never } & {