From: Tycho Date: Mon, 5 Aug 2024 02:59:44 +0000 (+0800) Subject: fix(types/withDefaults): ensure default values of type `any` do not include `undefine... X-Git-Tag: v3.4.36~23 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=4592b63c6a8a3d69bfe4ac1f9458b4a86a9676a4;p=thirdparty%2Fvuejs%2Fcore.git fix(types/withDefaults): ensure default values of type `any` do not include `undefined` (#11490) --- diff --git a/packages/dts-test/setupHelpers.test-d.ts b/packages/dts-test/setupHelpers.test-d.ts index 729f9b97d0..b134a4ca08 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 39d8edbcc2..c233fd350c 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 } & {