Skip to content

Commit

Permalink
fix(runtime-core) improve the type inference using withDefaults(vuejs…
Browse files Browse the repository at this point in the history
  • Loading branch information
双面胶 committed Sep 28, 2022
1 parent 96cd924 commit 1e53810
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
6 changes: 3 additions & 3 deletions packages/runtime-core/src/apiSetupHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,9 @@ type InferDefault<P, T> = T extends
? T | ((props: P) => T)
: (props: P) => T

type PropsWithDefaults<Base, Defaults> = Base & {
[K in keyof Defaults]: K extends keyof Base ? NotUndefined<Base[K]> : never
}
type PropsWithDefaults<Base, Defaults> = Base & ({
[K in keyof Defaults]: K extends keyof Base ?Defaults[K]& NotUndefined<Base[K]> : never
})

/**
* Vue `<script setup>` compiler macro for providing props default values when
Expand Down
6 changes: 5 additions & 1 deletion test-dts/setupHelpers.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@ describe('defineProps w/ type declaration + withDefaults', () => {
fn?: (e: string) => void
x?: string
genStr?: string
y?: string
}>(),
{
number: 123,
arr: () => [],
obj: () => ({ x: 123 }),
fn: () => {},
genStr: () => ''
genStr: () => '',
y:undefined
}
)

Expand All @@ -46,6 +48,8 @@ describe('defineProps w/ type declaration + withDefaults', () => {
// @ts-expect-error
res.x.slice()
res.genStr.slice()
// @ts-expect-error
res.y.split('')
})

describe('defineProps w/ union type declaration + withDefaults', () => {
Expand Down

0 comments on commit 1e53810

Please sign in to comment.