Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(types): propType<any> type infer #4985

Merged
merged 3 commits into from Nov 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/runtime-core/src/componentProps.ts
Expand Up @@ -39,6 +39,7 @@ import { createPropsDefaultThis } from './compat/props'
import { isCompatEnabled, softAssertCompatEnabled } from './compat/compatConfig'
import { DeprecationTypes } from './compat/compatConfig'
import { shouldSkipAttr } from './compat/attrsFallthrough'
import { IfAny } from './helpers/typeUtils'

export type ComponentPropsOptions<P = Data> =
| ComponentObjectPropsOptions<P>
Expand Down Expand Up @@ -115,7 +116,7 @@ type InferPropType<T> = [T] extends [null]
: InferPropType<U>
: [T] extends [Prop<infer V, infer D>]
? unknown extends V
? D
? IfAny<V, V, D>
: V
: T

Expand Down
2 changes: 2 additions & 0 deletions packages/runtime-core/src/helpers/typeUtils.ts
Expand Up @@ -6,3 +6,5 @@ export type UnionToIntersection<U> = (

// make keys required but keep undefined values
export type LooseRequired<T> = { [P in string & keyof T]: T[P] }

export type IfAny<T, Y, N> = 0 extends (1 & T) ? Y : N
8 changes: 6 additions & 2 deletions test-dts/component.test-d.ts
Expand Up @@ -10,7 +10,8 @@ import {
ShallowUnwrapRef,
FunctionalComponent,
ComponentPublicInstance,
toRefs
toRefs,
IsAny
} from './index'

declare function extractComponentOptions<Props, RawBindings>(
Expand Down Expand Up @@ -62,6 +63,7 @@ describe('object props', () => {
ffff: Ref<(a: number, b: string) => { a: boolean }>
validated: Ref<string | undefined>
object: Ref<object | undefined>
zzz: any
}

describe('defineComponent', () => {
Expand Down Expand Up @@ -130,7 +132,8 @@ describe('object props', () => {
// validator requires explicit annotation
validator: (val: unknown) => val !== ''
},
object: Object as PropType<object>
object: Object as PropType<object>,
zzz: Object as PropType<any>
},
setup(props) {
const refs = toRefs(props)
Expand All @@ -152,6 +155,7 @@ describe('object props', () => {
expectType<ExpectedRefs['ffff']>(refs.ffff)
expectType<ExpectedRefs['validated']>(refs.validated)
expectType<ExpectedRefs['object']>(refs.object)
expectType<IsAny<typeof props.zzz>>(true)

return {
setupA: 1,
Expand Down
2 changes: 2 additions & 0 deletions test-dts/index.d.ts
Expand Up @@ -14,3 +14,5 @@ export type IsUnion<T, U extends T = T> = (T extends any
: never) extends false
? false
: true

export type IsAny<T> = 0 extends (1 & T) ? true : false