From 674c297e36ff9c6479e36829fc34bea3852c6682 Mon Sep 17 00:00:00 2001 From: KazariEX <1364035137@qq.com> Date: Sun, 29 Sep 2024 17:17:01 +0800 Subject: [PATCH] fix(type): correctly infer `TypeProps` when it is `any` --- .../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..687e674d7eb 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 = unknown extends T + ? false + : keyof T extends K + ? true + : false + /** * Utility for extracting the parameters from a function overload (for typed emits) * https://github.com/microsoft/TypeScript/issues/32164#issuecomment-1146737709