Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(type): fix nullabe props not getting undefined union #12103

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions packages-private/dts-test/setupHelpers.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ describe('defineProps w/ type declaration', () => {
// type declaration
const props = defineProps<{
foo: string
bool?: boolean
bool: boolean
boolOptional?: boolean
boolAndUndefined: boolean | undefined
file?: File | File[]
}>()
Expand All @@ -30,15 +31,22 @@ describe('defineProps w/ type declaration', () => {
props.bar

expectType<boolean>(props.bool)
expectType<boolean>(props.boolAndUndefined)
expectType<boolean | undefined>(props.boolOptional)
expectType<boolean | undefined>(props.boolAndUndefined)
})

describe('defineProps w/ generics', () => {
function test<T extends boolean>() {
const props = defineProps<{ foo: T; bar: string; x?: boolean }>()
const props = defineProps<{
foo: T
bar: string
x: boolean
xOptional?: boolean
}>()
expectType<T>(props.foo)
expectType<string>(props.bar)
expectType<boolean>(props.x)
expectType<boolean | undefined>(props.xOptional)
}
test()
})
Expand Down
2 changes: 1 addition & 1 deletion packages/runtime-core/src/apiSetupHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export function defineProps() {
}

export type DefineProps<T, BKeys extends keyof T> = Readonly<T> & {
readonly [K in BKeys]-?: boolean
readonly [K in BKeys]-?: T[K]
Copy link
Member

@edison1105 edison1105 Oct 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test cases above can pass without this change.

see #12098 (comment)

}

type BooleanKey<T, K extends keyof T = keyof T> = K extends any
Expand Down