Skip to content

Commit

Permalink
fix: Date infer string in props (#627)
Browse files Browse the repository at this point in the history
  • Loading branch information
Zcating committed Jan 14, 2021
1 parent 5301d49 commit b2acb2d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/component/componentProps.ts
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
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

0 comments on commit b2acb2d

Please sign in to comment.