Skip to content

Commit

Permalink
fix(types/withDefaults): ensure default values of type any do not i…
Browse files Browse the repository at this point in the history
…nclude `undefined`
  • Loading branch information
jh-leong committed Aug 5, 2024
1 parent 2a29a71 commit 8e7260d
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
6 changes: 5 additions & 1 deletion packages/dts-test/setupHelpers.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -55,6 +56,7 @@ describe('defineProps w/ type declaration + withDefaults', () => {
z?: string
bool?: boolean
boolAndUndefined: boolean | undefined
foo?: T
}>(),
{
number: 123,
Expand All @@ -64,6 +66,7 @@ describe('defineProps w/ type declaration + withDefaults', () => {
genStr: () => '',
y: undefined,
z: 'string',
foo: '' as any,
},
)

Expand All @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion packages/runtime-core/src/apiSetupHelpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
type IfAny,
type LooseRequired,
type Prettify,
type UnionToIntersection,
Expand Down Expand Up @@ -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
} & {
Expand Down

0 comments on commit 8e7260d

Please sign in to comment.