]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(types): fix propType<any> type inference (#4985)
authorfishDog <40156382+Bigfish8@users.noreply.github.com>
Thu, 25 Nov 2021 09:52:13 +0000 (17:52 +0800)
committerGitHub <noreply@github.com>
Thu, 25 Nov 2021 09:52:13 +0000 (04:52 -0500)
fix #4983

packages/runtime-core/src/componentProps.ts
packages/runtime-core/src/helpers/typeUtils.ts
test-dts/component.test-d.ts
test-dts/index.d.ts

index 924f40a7384cf87a410391ee3ffc2bf12e6fcc14..c999492225c02525885178efece68a45c6d71ded 100644 (file)
@@ -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>
@@ -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
 
index 204543e6de211649134d6b3ed0ed09595715c038..8caba54c6cae44f61c0643127ebb74f6facdb1d2 100644 (file)
@@ -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
index 93c3ea4109cfed2b5c990be6442f689b7e11767a..d8d3934b964230b4e3147e3cd6be6d318d42397e 100644 (file)
@@ -10,7 +10,8 @@ import {
   ShallowUnwrapRef,
   FunctionalComponent,
   ComponentPublicInstance,
-  toRefs
+  toRefs,
+  IsAny
 } from './index'
 
 declare function extractComponentOptions<Props, RawBindings>(
@@ -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', () => {
@@ -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)
@@ -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,
index 3d8d288fa6d077879d5a97b9d73ff345e3acf32f..59eadcb92d9075a66aff99cc41e0227200c8e56c 100644 (file)
@@ -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