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(types): avoid marking PropType<boolean> as undefined in props #12326

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
19 changes: 19 additions & 0 deletions packages-private/dts-test/defineComponent.test-d.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1841,6 +1841,25 @@ defineComponent({
},
})

describe('object props w/ PropType<boolean>', () => {
const MyComponent = defineComponent({
props: {
b1: { type: [Boolean] as PropType<boolean> },
b2: {
type: [Boolean, String] as PropType<boolean | string>,
required: true,
},
},
setup(props) {
expectType<boolean>(props.b1)
expectType<boolean | string>(props.b2)
},
})
expectType<JSX.Element>(<MyComponent b1 b2 />)
// @ts-expect-error
;<MyComponent />
})

import type * as vue from 'vue'

interface ErrorMessageSlotProps {
Expand Down
7 changes: 7 additions & 0 deletions packages-private/dts-test/setupHelpers.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
type PropType,
type Ref,
type Slots,
type VNode,
Expand Down Expand Up @@ -269,11 +270,17 @@ describe('defineProps w/ runtime declaration', () => {
type: Array,
required: true,
},
b1: Boolean as PropType<boolean>,
b2: Boolean as PropType<any>,
b3: Number as PropType<number>,
})
expectType<{
foo?: string
bar: number
baz: unknown[]
b1: boolean
b2?: any
b3?: number
}>(props)

props.foo && props.foo + 'bar'
Expand Down
10 changes: 8 additions & 2 deletions packages/runtime-core/src/componentProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ type RequiredKeys<T> = {
? T[K] extends { default: undefined | (() => undefined) }
? never
: K
: never
: T[K] extends PropType<infer B> | { type: PropType<infer B> }
? IfAny<B, never, B extends boolean ? K : never>
: never
}[keyof T]

type OptionalKeys<T> = Exclude<keyof T, RequiredKeys<T>>
Expand All @@ -104,7 +106,11 @@ type DefaultKeys<T> = {
? T[K] extends { type: BooleanConstructor; required: true } // not default if Boolean is marked as required
? never
: K
: never
: T[K] extends
| PropType<infer B>
| { type: PropType<infer B>; required?: false }
? IfAny<B, never, B extends boolean ? K : never>
: never
}[keyof T]

type InferPropType<T, NullAsAny = true> = [T] extends [null]
Expand Down