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: correct prop type inference when using PropType<unknown> #825

Merged
merged 1 commit into from Oct 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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