Skip to content

Commit 622e07d

Browse files
committed
fix(types): correct handling of required props with undefined type
1 parent 650ea4f commit 622e07d

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

packages-private/dts-test/defineComponent.test-d.tsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ describe('with object props', () => {
2222
a?: number | undefined
2323
aa: number
2424
aaa: number | null
25+
aaaa: number | undefined
2526
b: string
2627
e?: Function
2728
h: boolean
@@ -63,6 +64,11 @@ describe('with object props', () => {
6364
type: Number as PropType<number | null>,
6465
default: 1,
6566
},
67+
aaaa: {
68+
type: Number as PropType<number | undefined>,
69+
// `as const` prevents widening to `boolean` (keeps literal `true` type)
70+
required: true as const,
71+
},
6672
// required should make property non-void
6773
b: {
6874
type: String,
@@ -158,6 +164,11 @@ describe('with object props', () => {
158164
expectType<ExpectedProps['a']>(props.a)
159165
expectType<ExpectedProps['aa']>(props.aa)
160166
expectType<ExpectedProps['aaa']>(props.aaa)
167+
168+
// @ts-expect-error should included `undefined`
169+
expectType<number>(props.aaaa)
170+
expectType<ExpectedProps['aaaa']>(props.aaaa)
171+
161172
expectType<ExpectedProps['b']>(props.b)
162173
expectType<ExpectedProps['e']>(props.e)
163174
expectType<ExpectedProps['h']>(props.h)
@@ -285,6 +296,7 @@ describe('with object props', () => {
285296
expectType<JSX.Element>(
286297
<MyComponent
287298
a={1}
299+
aaaa={1}
288300
b="b"
289301
bb="bb"
290302
e={() => {}}
@@ -311,6 +323,7 @@ describe('with object props', () => {
311323

312324
expectType<Component>(
313325
<MyComponent
326+
aaaa={1}
314327
b="b"
315328
dd={{ n: 1 }}
316329
ddd={['ddd']}

packages/runtime-core/src/componentProps.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,9 @@ type InferPropType<T, NullAsAny = true> = [T] extends [null]
143143
export type ExtractPropTypes<O> = {
144144
// use `keyof Pick<O, RequiredKeys<O>>` instead of `RequiredKeys<O>` to
145145
// support IDE features
146-
[K in keyof Pick<O, RequiredKeys<O>>]: Exclude<InferPropType<O[K]>, undefined>
146+
[K in keyof Pick<O, RequiredKeys<O>>]: O[K] extends { default: any }
147+
? Exclude<InferPropType<O[K]>, undefined>
148+
: InferPropType<O[K]>
147149
} & {
148150
// use `keyof Pick<O, OptionalKeys<O>>` instead of `OptionalKeys<O>` to
149151
// support IDE features

0 commit comments

Comments
 (0)