From 57315ab9688c9741a271d1075bbd28cbe5f71e2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B1=B1=E5=90=B9=E8=89=B2=E5=BE=A1=E5=AE=88?= <85992002+KazariEX@users.noreply.github.com> Date: Thu, 3 Oct 2024 23:22:27 +0800 Subject: [PATCH] fix(types): correctly infer `TypeProps` when it is `any` (#12073) close #12058 --- .../runtime-core/src/apiDefineComponent.ts | 18 ++++++++---------- packages/shared/src/typeUtils.ts | 6 ++++++ 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/packages/runtime-core/src/apiDefineComponent.ts b/packages/runtime-core/src/apiDefineComponent.ts index 3748fc81c63..d5e2feb5e6d 100644 --- a/packages/runtime-core/src/apiDefineComponent.ts +++ b/packages/runtime-core/src/apiDefineComponent.ts @@ -27,7 +27,7 @@ import type { EmitsToProps, TypeEmitsToOptions, } from './componentEmits' -import { extend, isFunction } from '@vue/shared' +import { type IsKeyValues, extend, isFunction } from '@vue/shared' import type { VNodeProps } from './vnode' import type { ComponentPublicInstanceConstructor, @@ -208,15 +208,13 @@ export function defineComponent< ResolvedEmits extends EmitsOptions = {} extends RuntimeEmitsOptions ? TypeEmitsToOptions : RuntimeEmitsOptions, - InferredProps = unknown extends TypeProps - ? keyof TypeProps extends never - ? string extends RuntimePropsKeys - ? ComponentObjectPropsOptions extends RuntimePropsOptions - ? {} - : ExtractPropTypes - : { [key in RuntimePropsKeys]?: any } - : TypeProps - : TypeProps, + InferredProps = IsKeyValues extends true + ? TypeProps + : string extends RuntimePropsKeys + ? ComponentObjectPropsOptions extends RuntimePropsOptions + ? {} + : ExtractPropTypes + : { [key in RuntimePropsKeys]?: any }, TypeRefs extends Record = {}, TypeEl extends Element = any, >( diff --git a/packages/shared/src/typeUtils.ts b/packages/shared/src/typeUtils.ts index 1fd161ceb05..f5b9e6ec3c8 100644 --- a/packages/shared/src/typeUtils.ts +++ b/packages/shared/src/typeUtils.ts @@ -13,6 +13,12 @@ export type LooseRequired = { [P in keyof (T & Required)]: T[P] } // https://stackoverflow.com/questions/49927523/disallow-call-with-any/49928360#49928360 export type IfAny = 0 extends 1 & T ? Y : N +export type IsKeyValues = IfAny< + T, + false, + T extends object ? (keyof T extends K ? true : false) : false +> + /** * Utility for extracting the parameters from a function overload (for typed emits) * https://github.com/microsoft/TypeScript/issues/32164#issuecomment-1146737709