fix(types): retain union type narrowing with defaults applied (#12108)
close #12106
This commit is contained in:
parent
cde2c0671b
commit
05685a9d7c
|
@ -240,6 +240,23 @@ describe('withDefaults w/ defineProp type is different from the defaults type',
|
||||||
res1.value
|
res1.value
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe('withDefaults w/ defineProp discriminate union type', () => {
|
||||||
|
const props = withDefaults(
|
||||||
|
defineProps<
|
||||||
|
{ type: 'button'; buttonType?: 'submit' } | { type: 'link'; href: string }
|
||||||
|
>(),
|
||||||
|
{
|
||||||
|
type: 'button',
|
||||||
|
},
|
||||||
|
)
|
||||||
|
if (props.type === 'button') {
|
||||||
|
expectType<'submit' | undefined>(props.buttonType)
|
||||||
|
}
|
||||||
|
if (props.type === 'link') {
|
||||||
|
expectType<string>(props.href)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
describe('defineProps w/ runtime declaration', () => {
|
describe('defineProps w/ runtime declaration', () => {
|
||||||
// runtime declaration
|
// runtime declaration
|
||||||
const props = defineProps({
|
const props = defineProps({
|
||||||
|
|
|
@ -331,7 +331,8 @@ type PropsWithDefaults<
|
||||||
T,
|
T,
|
||||||
Defaults extends InferDefaults<T>,
|
Defaults extends InferDefaults<T>,
|
||||||
BKeys extends keyof T,
|
BKeys extends keyof T,
|
||||||
> = Readonly<MappedOmit<T, keyof Defaults>> & {
|
> = T extends unknown
|
||||||
|
? Readonly<MappedOmit<T, keyof Defaults>> & {
|
||||||
readonly [K in keyof Defaults as K extends keyof T
|
readonly [K in keyof Defaults as K extends keyof T
|
||||||
? K
|
? K
|
||||||
: never]-?: K extends keyof T
|
: never]-?: K extends keyof T
|
||||||
|
@ -346,6 +347,7 @@ type PropsWithDefaults<
|
||||||
: boolean
|
: boolean
|
||||||
: boolean
|
: boolean
|
||||||
}
|
}
|
||||||
|
: never
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Vue `<script setup>` compiler macro for providing props default values when
|
* Vue `<script setup>` compiler macro for providing props default values when
|
||||||
|
|
Loading…
Reference in New Issue