fix(types): fix propType<any> type inference (#4985)

fix #4983
This commit is contained in:
fishDog 2021-11-25 17:52:13 +08:00 committed by GitHub
parent c17cbdc28f
commit 3c449cd408
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 12 additions and 3 deletions

View File

@ -39,6 +39,7 @@ import { createPropsDefaultThis } from './compat/props'
import { isCompatEnabled, softAssertCompatEnabled } from './compat/compatConfig' import { isCompatEnabled, softAssertCompatEnabled } from './compat/compatConfig'
import { DeprecationTypes } from './compat/compatConfig' import { DeprecationTypes } from './compat/compatConfig'
import { shouldSkipAttr } from './compat/attrsFallthrough' import { shouldSkipAttr } from './compat/attrsFallthrough'
import { IfAny } from './helpers/typeUtils'
export type ComponentPropsOptions<P = Data> = export type ComponentPropsOptions<P = Data> =
| ComponentObjectPropsOptions<P> | ComponentObjectPropsOptions<P>
@ -115,7 +116,7 @@ type InferPropType<T> = [T] extends [null]
: 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 ? IfAny<V, V, D>
: V : V
: T : T

View File

@ -6,3 +6,5 @@ export type UnionToIntersection<U> = (
// make keys required but keep undefined values // make keys required but keep undefined values
export type LooseRequired<T> = { [P in string & keyof T]: T[P] } export type LooseRequired<T> = { [P in string & keyof T]: T[P] }
export type IfAny<T, Y, N> = 0 extends (1 & T) ? Y : N

View File

@ -10,7 +10,8 @@ import {
ShallowUnwrapRef, ShallowUnwrapRef,
FunctionalComponent, FunctionalComponent,
ComponentPublicInstance, ComponentPublicInstance,
toRefs toRefs,
IsAny
} from './index' } from './index'
declare function extractComponentOptions<Props, RawBindings>( declare function extractComponentOptions<Props, RawBindings>(
@ -62,6 +63,7 @@ describe('object props', () => {
ffff: Ref<(a: number, b: string) => { a: boolean }> ffff: Ref<(a: number, b: string) => { a: boolean }>
validated: Ref<string | undefined> validated: Ref<string | undefined>
object: Ref<object | undefined> object: Ref<object | undefined>
zzz: any
} }
describe('defineComponent', () => { describe('defineComponent', () => {
@ -130,7 +132,8 @@ describe('object props', () => {
// validator requires explicit annotation // validator requires explicit annotation
validator: (val: unknown) => val !== '' validator: (val: unknown) => val !== ''
}, },
object: Object as PropType<object> object: Object as PropType<object>,
zzz: Object as PropType<any>
}, },
setup(props) { setup(props) {
const refs = toRefs(props) const refs = toRefs(props)
@ -152,6 +155,7 @@ describe('object props', () => {
expectType<ExpectedRefs['ffff']>(refs.ffff) expectType<ExpectedRefs['ffff']>(refs.ffff)
expectType<ExpectedRefs['validated']>(refs.validated) expectType<ExpectedRefs['validated']>(refs.validated)
expectType<ExpectedRefs['object']>(refs.object) expectType<ExpectedRefs['object']>(refs.object)
expectType<IsAny<typeof props.zzz>>(true)
return { return {
setupA: 1, setupA: 1,

2
test-dts/index.d.ts vendored
View File

@ -14,3 +14,5 @@ export type IsUnion<T, U extends T = T> = (T extends any
: never) extends false : never) extends false
? false ? false
: true : true
export type IsAny<T> = 0 extends (1 & T) ? true : false