Skip to content

Commit

Permalink
fix(types): fix propType<any> type inference (#4985)
Browse files Browse the repository at this point in the history
fix #4983
  • Loading branch information
Bigfish8 committed Nov 25, 2021
1 parent c17cbdc commit 3c449cd
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 3 deletions.
3 changes: 2 additions & 1 deletion packages/runtime-core/src/componentProps.ts
Expand Up @@ -39,6 +39,7 @@ import { createPropsDefaultThis } from './compat/props'
import { isCompatEnabled, softAssertCompatEnabled } from './compat/compatConfig'
import { DeprecationTypes } from './compat/compatConfig'
import { shouldSkipAttr } from './compat/attrsFallthrough'
import { IfAny } from './helpers/typeUtils'

export type ComponentPropsOptions<P = Data> =
| ComponentObjectPropsOptions<P>
Expand Down Expand Up @@ -115,7 +116,7 @@ type InferPropType<T> = [T] extends [null]
: InferPropType<U>
: [T] extends [Prop<infer V, infer D>]
? unknown extends V
? D
? IfAny<V, V, D>
: V
: T

Expand Down
2 changes: 2 additions & 0 deletions packages/runtime-core/src/helpers/typeUtils.ts
Expand Up @@ -6,3 +6,5 @@ export type UnionToIntersection<U> = (

// make keys required but keep undefined values
export type LooseRequired<T> = { [P in string & keyof T]: T[P] }

export type IfAny<T, Y, N> = 0 extends (1 & T) ? Y : N
8 changes: 6 additions & 2 deletions test-dts/component.test-d.ts
Expand Up @@ -10,7 +10,8 @@ import {
ShallowUnwrapRef,
FunctionalComponent,
ComponentPublicInstance,
toRefs
toRefs,
IsAny
} from './index'

declare function extractComponentOptions<Props, RawBindings>(
Expand Down Expand Up @@ -62,6 +63,7 @@ describe('object props', () => {
ffff: Ref<(a: number, b: string) => { a: boolean }>
validated: Ref<string | undefined>
object: Ref<object | undefined>
zzz: any
}

describe('defineComponent', () => {
Expand Down Expand Up @@ -130,7 +132,8 @@ describe('object props', () => {
// validator requires explicit annotation
validator: (val: unknown) => val !== ''
},
object: Object as PropType<object>
object: Object as PropType<object>,
zzz: Object as PropType<any>
},
setup(props) {
const refs = toRefs(props)
Expand All @@ -152,6 +155,7 @@ describe('object props', () => {
expectType<ExpectedRefs['ffff']>(refs.ffff)
expectType<ExpectedRefs['validated']>(refs.validated)
expectType<ExpectedRefs['object']>(refs.object)
expectType<IsAny<typeof props.zzz>>(true)

return {
setupA: 1,
Expand Down
2 changes: 2 additions & 0 deletions test-dts/index.d.ts
Expand Up @@ -14,3 +14,5 @@ export type IsUnion<T, U extends T = T> = (T extends any
: never) extends false
? false
: true

export type IsAny<T> = 0 extends (1 & T) ? true : false

0 comments on commit 3c449cd

Please sign in to comment.