Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix: correct prop type inference when using PropType<unknown> (#825)
  • Loading branch information
Djaler committed Oct 5, 2021
1 parent 1a1cf2f commit 9c9f8e8
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/component/componentProps.ts
Expand Up @@ -63,7 +63,9 @@ type InferPropType<T> = T extends null
? Function
: T extends Prop<infer V, infer D>
? unknown extends V
? D
? D extends null | undefined
? V
: D
: ExtractCorrectPropType<V>
: T

Expand Down
7 changes: 7 additions & 0 deletions test-dts/defineComponent.test-d.ts
Expand Up @@ -28,6 +28,7 @@ describe('with object props', () => {
ffff: (a: number, b: string) => { a: boolean }
validated?: string
date: Date
unknown: unknown
}

type GT = string & { __brand: unknown }
Expand Down Expand Up @@ -100,6 +101,10 @@ describe('with object props', () => {
type: Date,
required: true,
},
unknown: {
type: Object as PropType<unknown>,
default: null,
},
},
setup(props) {
// type assertion. See https://github.com/SamVerschueren/tsd
Expand All @@ -121,6 +126,7 @@ describe('with object props', () => {
expectType<ExpectedProps['ffff']>(props.ffff)
expectType<ExpectedProps['validated']>(props.validated)
expectType<ExpectedProps['date']>(props.date)
expectType<typeof props.unknown>({} as ExpectedProps['unknown'])

isNotAnyOrUndefined(props.a)
isNotAnyOrUndefined(props.b)
Expand Down Expand Up @@ -171,6 +177,7 @@ describe('with object props', () => {
expectType<ExpectedProps['hhh']>(props.hhh)
expectType<ExpectedProps['ffff']>(props.ffff)
expectType<ExpectedProps['validated']>(props.validated)
expectType<typeof props.unknown>({} as ExpectedProps['unknown'])

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

0 comments on commit 9c9f8e8

Please sign in to comment.