fix(type): fix prop type infer (#4530)

fix #4525
This commit is contained in:
fishDog 2021-09-07 03:24:15 +08:00 committed by GitHub
parent 642710eded
commit 4178d5d7d9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 5 deletions

View File

@ -109,10 +109,10 @@ type InferPropType<T> = [T] extends [null]
? boolean ? boolean
: [T] extends [DateConstructor | { type: DateConstructor }] : [T] extends [DateConstructor | { type: DateConstructor }]
? Date ? Date
: [T] extends [ : [T] extends [(infer U)[] | { type: (infer U)[] }]
(DateConstructor | infer U)[] | { type: (DateConstructor | infer U)[] } ? U extends DateConstructor
] ? Date | InferPropType<U>
? Date | InferPropType<U> : 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

@ -44,6 +44,7 @@ describe('with object props', () => {
date?: Date date?: Date
l?: Date l?: Date
ll?: Date | number ll?: Date | number
lll?: string | number
} }
type GT = string & { __brand: unknown } type GT = string & { __brand: unknown }
@ -135,7 +136,8 @@ describe('with object props', () => {
}, },
date: Date, date: Date,
l: [Date], l: [Date],
ll: [Date, Number] ll: [Date, Number],
lll: [String, Number]
}, },
setup(props) { setup(props) {
// type assertion. See https://github.com/SamVerschueren/tsd // type assertion. See https://github.com/SamVerschueren/tsd
@ -170,6 +172,7 @@ describe('with object props', () => {
expectType<ExpectedProps['date']>(props.date) expectType<ExpectedProps['date']>(props.date)
expectType<ExpectedProps['l']>(props.l) expectType<ExpectedProps['l']>(props.l)
expectType<ExpectedProps['ll']>(props.ll) expectType<ExpectedProps['ll']>(props.ll)
expectType<ExpectedProps['lll']>(props.lll)
// @ts-expect-error props should be readonly // @ts-expect-error props should be readonly
expectError((props.a = 1)) expectError((props.a = 1))