Skip to content

Commit 3c449cd

Browse files
authored
fix(types): fix propType<any> type inference (#4985)
fix #4983
1 parent c17cbdc commit 3c449cd

File tree

4 files changed

+12
-3
lines changed

4 files changed

+12
-3
lines changed

packages/runtime-core/src/componentProps.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import { createPropsDefaultThis } from './compat/props'
3939
import { isCompatEnabled, softAssertCompatEnabled } from './compat/compatConfig'
4040
import { DeprecationTypes } from './compat/compatConfig'
4141
import { shouldSkipAttr } from './compat/attrsFallthrough'
42+
import { IfAny } from './helpers/typeUtils'
4243

4344
export type ComponentPropsOptions<P = Data> =
4445
| ComponentObjectPropsOptions<P>
@@ -115,7 +116,7 @@ type InferPropType<T> = [T] extends [null]
115116
: InferPropType<U>
116117
: [T] extends [Prop<infer V, infer D>]
117118
? unknown extends V
118-
? D
119+
? IfAny<V, V, D>
119120
: V
120121
: T
121122

packages/runtime-core/src/helpers/typeUtils.ts

+2
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@ export type UnionToIntersection<U> = (
66

77
// make keys required but keep undefined values
88
export type LooseRequired<T> = { [P in string & keyof T]: T[P] }
9+
10+
export type IfAny<T, Y, N> = 0 extends (1 & T) ? Y : N

test-dts/component.test-d.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ import {
1010
ShallowUnwrapRef,
1111
FunctionalComponent,
1212
ComponentPublicInstance,
13-
toRefs
13+
toRefs,
14+
IsAny
1415
} from './index'
1516

1617
declare function extractComponentOptions<Props, RawBindings>(
@@ -62,6 +63,7 @@ describe('object props', () => {
6263
ffff: Ref<(a: number, b: string) => { a: boolean }>
6364
validated: Ref<string | undefined>
6465
object: Ref<object | undefined>
66+
zzz: any
6567
}
6668

6769
describe('defineComponent', () => {
@@ -130,7 +132,8 @@ describe('object props', () => {
130132
// validator requires explicit annotation
131133
validator: (val: unknown) => val !== ''
132134
},
133-
object: Object as PropType<object>
135+
object: Object as PropType<object>,
136+
zzz: Object as PropType<any>
134137
},
135138
setup(props) {
136139
const refs = toRefs(props)
@@ -152,6 +155,7 @@ describe('object props', () => {
152155
expectType<ExpectedRefs['ffff']>(refs.ffff)
153156
expectType<ExpectedRefs['validated']>(refs.validated)
154157
expectType<ExpectedRefs['object']>(refs.object)
158+
expectType<IsAny<typeof props.zzz>>(true)
155159

156160
return {
157161
setupA: 1,

test-dts/index.d.ts

+2
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,5 @@ export type IsUnion<T, U extends T = T> = (T extends any
1414
: never) extends false
1515
? false
1616
: true
17+
18+
export type IsAny<T> = 0 extends (1 & T) ? true : false

0 commit comments

Comments
 (0)