fix(types): retain union type narrowing with defaults applied (#12108)

close #12106
This commit is contained in:
edison 2024-10-11 11:17:15 +08:00 committed by Evan You
parent cde2c0671b
commit 05685a9d7c
No known key found for this signature in database
GPG Key ID: 00E9AB7A6704CE0A
2 changed files with 34 additions and 15 deletions

View File

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

View File

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