types(runtime-core): fix for multiple possible prop types including Date (#4518)

fix #4517
This commit is contained in:
Husam Ibrahim 2021-09-06 00:16:44 +02:00 committed by GitHub
parent fac9a2926d
commit 4645a42b79
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 1 deletions

View File

@ -109,6 +109,10 @@ type InferPropType<T> = [T] extends [null]
? boolean ? boolean
: [T] extends [DateConstructor | { type: DateConstructor }] : [T] extends [DateConstructor | { type: DateConstructor }]
? Date ? Date
: [T] extends [
(DateConstructor | infer U)[] | { type: (DateConstructor | infer U)[] }
]
? Date | InferPropType<U>
: [T] extends [Prop<infer V, infer D>] : [T] extends [Prop<infer V, infer D>]
? unknown extends V ? unknown extends V
? D ? D

View File

@ -42,6 +42,8 @@ describe('with object props', () => {
kkk?: any kkk?: any
validated?: string validated?: string
date?: Date date?: Date
l?: Date
ll?: Date | number
} }
type GT = string & { __brand: unknown } type GT = string & { __brand: unknown }
@ -131,7 +133,9 @@ describe('with object props', () => {
// validator requires explicit annotation // validator requires explicit annotation
validator: (val: unknown) => val !== '' validator: (val: unknown) => val !== ''
}, },
date: Date date: Date,
l: [Date],
ll: [Date, Number]
}, },
setup(props) { setup(props) {
// type assertion. See https://github.com/SamVerschueren/tsd // type assertion. See https://github.com/SamVerschueren/tsd
@ -164,6 +168,8 @@ describe('with object props', () => {
expectType<ExpectedProps['kkk']>(props.kkk) expectType<ExpectedProps['kkk']>(props.kkk)
expectType<ExpectedProps['validated']>(props.validated) expectType<ExpectedProps['validated']>(props.validated)
expectType<ExpectedProps['date']>(props.date) expectType<ExpectedProps['date']>(props.date)
expectType<ExpectedProps['l']>(props.l)
expectType<ExpectedProps['ll']>(props.ll)
// @ts-expect-error props should be readonly // @ts-expect-error props should be readonly
expectError((props.a = 1)) expectError((props.a = 1))