fix(types/withDefaults): ensure default values of type `any` do not include `undefined` (#11490)

This commit is contained in:
Tycho 2024-08-05 10:59:44 +08:00 committed by GitHub
parent 3430bffa4b
commit 4592b63c6a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 7 additions and 2 deletions

View File

@ -42,7 +42,8 @@ describe('defineProps w/ generics', () => {
test() test()
}) })
describe('defineProps w/ type declaration + withDefaults', () => { describe('defineProps w/ type declaration + withDefaults', <T extends
string>() => {
const res = withDefaults( const res = withDefaults(
defineProps<{ defineProps<{
number?: number number?: number
@ -55,6 +56,7 @@ describe('defineProps w/ type declaration + withDefaults', () => {
z?: string z?: string
bool?: boolean bool?: boolean
boolAndUndefined: boolean | undefined boolAndUndefined: boolean | undefined
foo?: T
}>(), }>(),
{ {
number: 123, number: 123,
@ -64,6 +66,7 @@ describe('defineProps w/ type declaration + withDefaults', () => {
genStr: () => '', genStr: () => '',
y: undefined, y: undefined,
z: 'string', z: 'string',
foo: '' as any,
}, },
) )
@ -80,6 +83,7 @@ describe('defineProps w/ type declaration + withDefaults', () => {
expectType<string | undefined>(res.x) expectType<string | undefined>(res.x)
expectType<string | undefined>(res.y) expectType<string | undefined>(res.y)
expectType<string>(res.z) expectType<string>(res.z)
expectType<T>(res.foo)
expectType<boolean>(res.bool) expectType<boolean>(res.bool)
expectType<boolean>(res.boolAndUndefined) expectType<boolean>(res.boolAndUndefined)

View File

@ -1,4 +1,5 @@
import { import {
type IfAny,
type LooseRequired, type LooseRequired,
type Prettify, type Prettify,
type UnionToIntersection, type UnionToIntersection,
@ -305,7 +306,7 @@ type PropsWithDefaults<
> = Readonly<MappedOmit<T, keyof Defaults>> & { > = Readonly<MappedOmit<T, keyof Defaults>> & {
readonly [K in keyof Defaults]-?: K extends keyof T readonly [K in keyof Defaults]-?: K extends keyof T
? Defaults[K] extends undefined ? Defaults[K] extends undefined
? T[K] ? IfAny<Defaults[K], NotUndefined<T[K]>, T[K]>
: NotUndefined<T[K]> : NotUndefined<T[K]>
: never : never
} & { } & {