From: fishDog <40156382+Bigfish8@users.noreply.github.com> Date: Thu, 25 Nov 2021 09:52:13 +0000 (+0800) Subject: fix(types): fix propType type inference (#4985) X-Git-Tag: v3.2.23~13 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3c449cd408840d35987fb32b39737fbf093809d6;p=thirdparty%2Fvuejs%2Fcore.git fix(types): fix propType type inference (#4985) fix #4983 --- diff --git a/packages/runtime-core/src/componentProps.ts b/packages/runtime-core/src/componentProps.ts index 924f40a738..c999492225 100644 --- a/packages/runtime-core/src/componentProps.ts +++ b/packages/runtime-core/src/componentProps.ts @@ -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

= | ComponentObjectPropsOptions

@@ -115,7 +116,7 @@ type InferPropType = [T] extends [null] : InferPropType : [T] extends [Prop] ? unknown extends V - ? D + ? IfAny : V : T diff --git a/packages/runtime-core/src/helpers/typeUtils.ts b/packages/runtime-core/src/helpers/typeUtils.ts index 204543e6de..8caba54c6c 100644 --- a/packages/runtime-core/src/helpers/typeUtils.ts +++ b/packages/runtime-core/src/helpers/typeUtils.ts @@ -6,3 +6,5 @@ export type UnionToIntersection = ( // make keys required but keep undefined values export type LooseRequired = { [P in string & keyof T]: T[P] } + +export type IfAny = 0 extends (1 & T) ? Y : N diff --git a/test-dts/component.test-d.ts b/test-dts/component.test-d.ts index 93c3ea4109..d8d3934b96 100644 --- a/test-dts/component.test-d.ts +++ b/test-dts/component.test-d.ts @@ -10,7 +10,8 @@ import { ShallowUnwrapRef, FunctionalComponent, ComponentPublicInstance, - toRefs + toRefs, + IsAny } from './index' declare function extractComponentOptions( @@ -62,6 +63,7 @@ describe('object props', () => { ffff: Ref<(a: number, b: string) => { a: boolean }> validated: Ref object: Ref + 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 as PropType, + zzz: Object as PropType }, setup(props) { const refs = toRefs(props) @@ -152,6 +155,7 @@ describe('object props', () => { expectType(refs.ffff) expectType(refs.validated) expectType(refs.object) + expectType>(true) return { setupA: 1, diff --git a/test-dts/index.d.ts b/test-dts/index.d.ts index 3d8d288fa6..59eadcb92d 100644 --- a/test-dts/index.d.ts +++ b/test-dts/index.d.ts @@ -14,3 +14,5 @@ export type IsUnion = (T extends any : never) extends false ? false : true + +export type IsAny = 0 extends (1 & T) ? true : false