]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(types/withDefaults): ensure default values of type `any` do not include `undefine...
authorTycho <jh.leong@outlook.com>
Mon, 5 Aug 2024 02:59:44 +0000 (10:59 +0800)
committerGitHub <noreply@github.com>
Mon, 5 Aug 2024 02:59:44 +0000 (10:59 +0800)
packages/dts-test/setupHelpers.test-d.ts
packages/runtime-core/src/apiSetupHelpers.ts

index 729f9b97d05f08381a22b8d73543e818c4114637..b134a4ca08ecde827b6edd1ebd1f54152bff5dba 100644 (file)
@@ -42,7 +42,8 @@ describe('defineProps w/ generics', () => {
   test()
 })
 
-describe('defineProps w/ type declaration + withDefaults', () => {
+describe('defineProps w/ type declaration + withDefaults', <T extends
+  string>() => {
   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<string | undefined>(res.x)
   expectType<string | undefined>(res.y)
   expectType<string>(res.z)
+  expectType<T>(res.foo)
 
   expectType<boolean>(res.bool)
   expectType<boolean>(res.boolAndUndefined)
index 39d8edbcc2061830dcb130327ac2a143a084f8dc..c233fd350cb74a2581aed24c4672bbedf7e2cf73 100644 (file)
@@ -1,4 +1,5 @@
 import {
+  type IfAny,
   type LooseRequired,
   type Prettify,
   type UnionToIntersection,
@@ -305,7 +306,7 @@ type PropsWithDefaults<
 > = Readonly<MappedOmit<T, keyof Defaults>> & {
   readonly [K in keyof Defaults]-?: K extends keyof T
     ? Defaults[K] extends undefined
-      ? T[K]
+      ? IfAny<Defaults[K], NotUndefined<T[K]>, T[K]>
       : NotUndefined<T[K]>
     : never
 } & {