mirror of
https://github.com/vuejs/vue.git
synced 2024-11-21 12:18:54 +00:00
fix(types/sfc): improve the type inference using withDefaults
(#12872)
This commit is contained in:
parent
67c1d26cb0
commit
099401e227
@ -19,15 +19,19 @@ describe('defineProps w/ type declaration + withDefaults', () => {
|
||||
arr?: string[]
|
||||
obj?: { x: number }
|
||||
fn?: (e: string) => void
|
||||
x?: string
|
||||
genStr?: string
|
||||
x?: string
|
||||
y?: string
|
||||
z?: string
|
||||
}>(),
|
||||
{
|
||||
number: 123,
|
||||
arr: () => [],
|
||||
obj: () => ({ x: 123 }),
|
||||
fn: () => {},
|
||||
genStr: () => ''
|
||||
genStr: () => '',
|
||||
y: undefined,
|
||||
z: 'string'
|
||||
}
|
||||
)
|
||||
|
||||
@ -35,9 +39,15 @@ describe('defineProps w/ type declaration + withDefaults', () => {
|
||||
res.arr.push('hi')
|
||||
res.obj.x
|
||||
res.fn('hi')
|
||||
res.genStr.slice()
|
||||
// @ts-expect-error
|
||||
res.x.slice()
|
||||
res.genStr.slice()
|
||||
// @ts-expect-error
|
||||
res.y.slice()
|
||||
|
||||
expectType<string | undefined>(res.x)
|
||||
expectType<string | undefined>(res.y)
|
||||
expectType<string>(res.z)
|
||||
})
|
||||
|
||||
describe('defineProps w/ union type declaration + withDefaults', () => {
|
||||
|
6
types/v3-setup-helpers.d.ts
vendored
6
types/v3-setup-helpers.d.ts
vendored
@ -110,7 +110,11 @@ type InferDefault<P, T> = T extends
|
||||
: (props: P) => T
|
||||
|
||||
type PropsWithDefaults<Base, Defaults> = Base & {
|
||||
[K in keyof Defaults]: K extends keyof Base ? NotUndefined<Base[K]> : never
|
||||
[K in keyof Defaults]: K extends keyof Base
|
||||
? Defaults[K] extends undefined
|
||||
? Base[K]
|
||||
: NotUndefined<Base[K]>
|
||||
: never
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user