Skip to content

Commit

Permalink
types(runtime-core): fix for multiple possible prop types including Date
Browse files Browse the repository at this point in the history
  • Loading branch information
HusamElbashir committed Sep 5, 2021
1 parent 3ad9dc4 commit 2759113
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
4 changes: 4 additions & 0 deletions packages/runtime-core/src/componentProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ type InferPropType<T> = [T] extends [null]
? boolean
: [T] extends [DateConstructor | { type: DateConstructor }]
? Date
: [T] extends [
(DateConstructor | infer U)[] | { type: (DateConstructor | infer U)[] }
]
? Date | InferPropType<U>
: [T] extends [Prop<infer V, infer D>]
? unknown extends V
? D
Expand Down
8 changes: 7 additions & 1 deletion test-dts/defineComponent.test-d.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ describe('with object props', () => {
kkk?: any
validated?: string
date?: Date
l?: Date
ll?: Date | number
}

type GT = string & { __brand: unknown }
Expand Down Expand Up @@ -131,7 +133,9 @@ describe('with object props', () => {
// validator requires explicit annotation
validator: (val: unknown) => val !== ''
},
date: Date
date: Date,
l: [Date],
ll: [Date, Number]
},
setup(props) {
// type assertion. See https://github.com/SamVerschueren/tsd
Expand Down Expand Up @@ -164,6 +168,8 @@ describe('with object props', () => {
expectType<ExpectedProps['kkk']>(props.kkk)
expectType<ExpectedProps['validated']>(props.validated)
expectType<ExpectedProps['date']>(props.date)
expectType<ExpectedProps['l']>(props.l)
expectType<ExpectedProps['ll']>(props.ll)

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

0 comments on commit 2759113

Please sign in to comment.