Skip to content

fix: Date infer string in props #627

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

Merged
merged 1 commit into from
Jan 14, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions src/component/componentProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,15 @@ type InferPropType<T> = T extends null
? Record<string, any>
: T extends BooleanConstructor | { type: BooleanConstructor }
? boolean
: T extends FunctionConstructor
? Function
: T extends Prop<infer V, infer D>
? unknown extends V
? D
: ExtractCorrectPropType<V>
: T
: T extends DateConstructor | { type: DateConstructor}
? Date
: T extends FunctionConstructor
? Function
: T extends Prop<infer V, infer D>
? unknown extends V
? D
: ExtractCorrectPropType<V>
: T

export type ExtractPropTypes<O> = O extends object
? { [K in RequiredKeys<O>]: InferPropType<O[K]> } &
Expand Down
6 changes: 6 additions & 0 deletions test-dts/defineComponent.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ describe('with object props', () => {
ggg: 'foo' | 'bar'
ffff: (a: number, b: string) => { a: boolean }
validated?: string
date: Date
}

type GT = string & { __brand: unknown }
Expand Down Expand Up @@ -96,6 +97,10 @@ describe('with object props', () => {
// validator requires explicit annotation
validator: (val: unknown) => val !== '',
},
date: {
type: Date,
required: true,
},
},
setup(props) {
// type assertion. See https://github.com/SamVerschueren/tsd
Expand All @@ -116,6 +121,7 @@ describe('with object props', () => {
expectType<ExpectedProps['hhh']>(props.hhh)
expectType<ExpectedProps['ffff']>(props.ffff)
expectType<ExpectedProps['validated']>(props.validated)
expectType<ExpectedProps['date']>(props.date)

isNotAnyOrUndefined(props.a)
isNotAnyOrUndefined(props.b)
Expand Down