fix(sfc/types): allow use default factory for primitive types in withDefaults (#5939)

fix #5938
This commit is contained in:
Alex Kozack 2022-05-23 03:28:39 +03:00 committed by GitHub
parent dddbd96dfe
commit b5462822d6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 3 deletions

View File

@ -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 & {

View File

@ -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: () => ''
}
)
@ -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', () => {
@ -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,
}
)
})