From 3c449cd408840d35987fb32b39737fbf093809d6 Mon Sep 17 00:00:00 2001 From: fishDog <40156382+Bigfish8@users.noreply.github.com> Date: Thu, 25 Nov 2021 17:52:13 +0800 Subject: [PATCH] fix(types): fix propType type inference (#4985) fix #4983 --- packages/runtime-core/src/componentProps.ts | 3 ++- packages/runtime-core/src/helpers/typeUtils.ts | 2 ++ test-dts/component.test-d.ts | 8 ++++++-- test-dts/index.d.ts | 2 ++ 4 files changed, 12 insertions(+), 3 deletions(-) diff --git a/packages/runtime-core/src/componentProps.ts b/packages/runtime-core/src/componentProps.ts index 924f40a7..c9994922 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 204543e6..8caba54c 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 93c3ea41..d8d3934b 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 3d8d288f..59eadcb9 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