Skip to content

Commit 57315ab

Browse files
authoredOct 3, 2024··
fix(types): correctly infer TypeProps when it is any (#12073)
close #12058
1 parent 577edca commit 57315ab

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed
 

‎packages/runtime-core/src/apiDefineComponent.ts

+8-10
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import type {
2727
EmitsToProps,
2828
TypeEmitsToOptions,
2929
} from './componentEmits'
30-
import { extend, isFunction } from '@vue/shared'
30+
import { type IsKeyValues, extend, isFunction } from '@vue/shared'
3131
import type { VNodeProps } from './vnode'
3232
import type {
3333
ComponentPublicInstanceConstructor,
@@ -208,15 +208,13 @@ export function defineComponent<
208208
ResolvedEmits extends EmitsOptions = {} extends RuntimeEmitsOptions
209209
? TypeEmitsToOptions<TypeEmits>
210210
: RuntimeEmitsOptions,
211-
InferredProps = unknown extends TypeProps
212-
? keyof TypeProps extends never
213-
? string extends RuntimePropsKeys
214-
? ComponentObjectPropsOptions extends RuntimePropsOptions
215-
? {}
216-
: ExtractPropTypes<RuntimePropsOptions>
217-
: { [key in RuntimePropsKeys]?: any }
218-
: TypeProps
219-
: TypeProps,
211+
InferredProps = IsKeyValues<TypeProps> extends true
212+
? TypeProps
213+
: string extends RuntimePropsKeys
214+
? ComponentObjectPropsOptions extends RuntimePropsOptions
215+
? {}
216+
: ExtractPropTypes<RuntimePropsOptions>
217+
: { [key in RuntimePropsKeys]?: any },
220218
TypeRefs extends Record<string, unknown> = {},
221219
TypeEl extends Element = any,
222220
>(

‎packages/shared/src/typeUtils.ts

+6
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ export type LooseRequired<T> = { [P in keyof (T & Required<T>)]: T[P] }
1313
// https://stackoverflow.com/questions/49927523/disallow-call-with-any/49928360#49928360
1414
export type IfAny<T, Y, N> = 0 extends 1 & T ? Y : N
1515

16+
export type IsKeyValues<T, K = string> = IfAny<
17+
T,
18+
false,
19+
T extends object ? (keyof T extends K ? true : false) : false
20+
>
21+
1622
/**
1723
* Utility for extracting the parameters from a function overload (for typed emits)
1824
* https://github.com/microsoft/TypeScript/issues/32164#issuecomment-1146737709

0 commit comments

Comments
 (0)
Please sign in to comment.