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,21 +331,23 @@ 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 [K in keyof Defaults as K extends keyof T ? Readonly<MappedOmit<T, keyof Defaults>> & {
? K readonly [K in keyof Defaults as K extends keyof T
: never]-?: K extends keyof T ? K
? Defaults[K] extends undefined : never]-?: K extends keyof T
? IfAny<Defaults[K], NotUndefined<T[K]>, T[K]> ? Defaults[K] extends undefined
: NotUndefined<T[K]> ? IfAny<Defaults[K], NotUndefined<T[K]>, T[K]>
: never : NotUndefined<T[K]>
} & { : never
readonly [K in BKeys]-?: K extends keyof Defaults } & {
? Defaults[K] extends undefined readonly [K in BKeys]-?: K extends keyof Defaults
? boolean | undefined ? Defaults[K] extends undefined
: boolean ? boolean | undefined
: 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