Skip to content

Commit

Permalink
fix(sfc/types): allow use default factory for primitive types in `wit…
Browse files Browse the repository at this point in the history
…hDefaults` (#5939)

fix #5938
  • Loading branch information
cawa-93 committed May 23, 2022
1 parent dddbd96 commit b546282
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
2 changes: 1 addition & 1 deletion packages/runtime-core/src/apiSetupHelpers.ts
Expand Up @@ -139,7 +139,7 @@ type InferDefault<P, T> = T extends
| boolean
| symbol
| Function
? T
? T | ((props: P) => T)
: (props: P) => T

type PropsWithDefaults<Base, Defaults> = Base & {
Expand Down
9 changes: 7 additions & 2 deletions test-dts/setupHelpers.test-d.ts
Expand Up @@ -28,12 +28,14 @@ describe('defineProps w/ type declaration + withDefaults', () => {
obj?: { x: number }
fn?: (e: string) => void
x?: string
genStr?: string
}>(),
{
number: 123,
arr: () => [],
obj: () => ({ x: 123 }),
fn: () => {}
fn: () => {},
genStr: () => ''
}
)

Expand All @@ -43,6 +45,7 @@ describe('defineProps w/ type declaration + withDefaults', () => {
res.fn('hi')
// @ts-expect-error
res.x.slice()
res.genStr.slice()
})

describe('defineProps w/ union type declaration + withDefaults', () => {
Expand All @@ -51,11 +54,13 @@ describe('defineProps w/ union type declaration + withDefaults', () => {
union1?: number | number[] | { x: number }
union2?: number | number[] | { x: number }
union3?: number | number[] | { x: number }
union4?: number | number[] | { x: number }
}>(),
{
union1: 123,
union2: () => [123],
union3: () => ({ x: 123 })
union3: () => ({ x: 123 }),
union4: () => 123,
}
)
})
Expand Down

0 comments on commit b546282

Please sign in to comment.