]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(types): correctly infer `TypeProps` when it is `any` (#12073)
author山吹色御守 <85992002+KazariEX@users.noreply.github.com>
Thu, 3 Oct 2024 15:22:27 +0000 (23:22 +0800)
committerGitHub <noreply@github.com>
Thu, 3 Oct 2024 15:22:27 +0000 (23:22 +0800)
close #12058

packages/runtime-core/src/apiDefineComponent.ts
packages/shared/src/typeUtils.ts

index 3748fc81c63929fbed5ee20a565e6086580f182a..d5e2feb5e6db32ca303342387107eea80c7da1c3 100644 (file)
@@ -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<TypeEmits>
     : RuntimeEmitsOptions,
-  InferredProps = unknown extends TypeProps
-    ? keyof TypeProps extends never
-      ? string extends RuntimePropsKeys
-        ? ComponentObjectPropsOptions extends RuntimePropsOptions
-          ? {}
-          : ExtractPropTypes<RuntimePropsOptions>
-        : { [key in RuntimePropsKeys]?: any }
-      : TypeProps
-    : TypeProps,
+  InferredProps = IsKeyValues<TypeProps> extends true
+    ? TypeProps
+    : string extends RuntimePropsKeys
+      ? ComponentObjectPropsOptions extends RuntimePropsOptions
+        ? {}
+        : ExtractPropTypes<RuntimePropsOptions>
+      : { [key in RuntimePropsKeys]?: any },
   TypeRefs extends Record<string, unknown> = {},
   TypeEl extends Element = any,
 >(
index 1fd161ceb05617890b9e62d03dc99a2040ec8836..f5b9e6ec3c8e22b5b4a60747ddd4578e20bfb492 100644 (file)
@@ -13,6 +13,12 @@ export type LooseRequired<T> = { [P in keyof (T & Required<T>)]: T[P] }
 // https://stackoverflow.com/questions/49927523/disallow-call-with-any/49928360#49928360
 export type IfAny<T, Y, N> = 0 extends 1 & T ? Y : N
 
+export type IsKeyValues<T, K = string> = 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