Skip to content

Commit 9c9f8e8

Browse files
authored
fix: correct prop type inference when using PropType<unknown> (#825)
1 parent 1a1cf2f commit 9c9f8e8

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

src/component/componentProps.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@ type InferPropType<T> = T extends null
6363
? Function
6464
: T extends Prop<infer V, infer D>
6565
? unknown extends V
66-
? D
66+
? D extends null | undefined
67+
? V
68+
: D
6769
: ExtractCorrectPropType<V>
6870
: T
6971

test-dts/defineComponent.test-d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ describe('with object props', () => {
2828
ffff: (a: number, b: string) => { a: boolean }
2929
validated?: string
3030
date: Date
31+
unknown: unknown
3132
}
3233

3334
type GT = string & { __brand: unknown }
@@ -100,6 +101,10 @@ describe('with object props', () => {
100101
type: Date,
101102
required: true,
102103
},
104+
unknown: {
105+
type: Object as PropType<unknown>,
106+
default: null,
107+
},
103108
},
104109
setup(props) {
105110
// type assertion. See https://github.com/SamVerschueren/tsd
@@ -121,6 +126,7 @@ describe('with object props', () => {
121126
expectType<ExpectedProps['ffff']>(props.ffff)
122127
expectType<ExpectedProps['validated']>(props.validated)
123128
expectType<ExpectedProps['date']>(props.date)
129+
expectType<typeof props.unknown>({} as ExpectedProps['unknown'])
124130

125131
isNotAnyOrUndefined(props.a)
126132
isNotAnyOrUndefined(props.b)
@@ -171,6 +177,7 @@ describe('with object props', () => {
171177
expectType<ExpectedProps['hhh']>(props.hhh)
172178
expectType<ExpectedProps['ffff']>(props.ffff)
173179
expectType<ExpectedProps['validated']>(props.validated)
180+
expectType<typeof props.unknown>({} as ExpectedProps['unknown'])
174181

175182
// @ts-expect-error props should be readonly
176183
expectError((props.a = 1))

0 commit comments

Comments
 (0)