fix(types): correct withDefaults return type for boolean prop with undefined default value (#8602)

This commit is contained in:
zqran 2023-07-11 18:35:22 +08:00 committed by GitHub
parent 6a22b1f6c2
commit f07cb18fed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 1 deletions

View File

@ -134,6 +134,26 @@ describe('defineProps w/ generic type declaration + withDefaults', <T extends nu
expectType<boolean>(res.bool)
})
describe('withDefaults w/ boolean type', () => {
const res1 = withDefaults(
defineProps<{
bool?: boolean
}>(),
{ bool: false }
)
expectType<boolean>(res1.bool)
const res2 = withDefaults(
defineProps<{
bool?: boolean
}>(),
{
bool: undefined
}
)
expectType<boolean | undefined>(res2.bool)
})
describe('defineProps w/ runtime declaration', () => {
// runtime declaration
const props = defineProps({

View File

@ -303,7 +303,13 @@ type PropsWithDefaults<
? T[K]
: NotUndefined<T[K]>
: never
} & { readonly [K in BKeys]-?: boolean }
} & {
readonly [K in BKeys]-?: K extends keyof Defaults
? Defaults[K] extends undefined
? boolean | undefined
: boolean
: boolean
}
/**
* Vue `<script setup>` compiler macro for providing props default values when