types(defineComponent): fix unwrap when returning Ref<T>|undefined
from setup
(#4100)
This commit is contained in:
parent
e508ee08e9
commit
f6a5f09a3a
@ -205,7 +205,11 @@ type BaseTypes = string | number | boolean
|
||||
export interface RefUnwrapBailTypes {}
|
||||
|
||||
export type ShallowUnwrapRef<T> = {
|
||||
[K in keyof T]: T[K] extends Ref<infer V> ? V : T[K]
|
||||
[K in keyof T]: T[K] extends Ref<infer V>
|
||||
? V
|
||||
: T[K] extends Ref<infer V> | undefined // if `V` is `unknown` that means it does not extend `Ref` and is undefined
|
||||
? unknown extends V ? undefined : V | undefined
|
||||
: T[K]
|
||||
}
|
||||
|
||||
export type UnwrapRef<T> = T extends Ref<infer V>
|
||||
|
@ -159,6 +159,7 @@ describe('object props', () => {
|
||||
setupC: {
|
||||
a: ref(2)
|
||||
},
|
||||
setupD: undefined as Ref<number> | undefined,
|
||||
setupProps: props
|
||||
}
|
||||
}
|
||||
@ -190,7 +191,7 @@ describe('object props', () => {
|
||||
expectType<Number>(rawBindings.setupA)
|
||||
expectType<Ref<Number>>(rawBindings.setupB)
|
||||
expectType<Ref<Number>>(rawBindings.setupC.a)
|
||||
expectType<Number>(rawBindings.setupA)
|
||||
expectType<Ref<Number> | undefined>(rawBindings.setupD)
|
||||
|
||||
// raw bindings props
|
||||
expectType<ExpectedProps['a']>(rawBindings.setupProps.a)
|
||||
@ -215,7 +216,7 @@ describe('object props', () => {
|
||||
expectType<Number>(setup.setupA)
|
||||
expectType<Number>(setup.setupB)
|
||||
expectType<Ref<Number>>(setup.setupC.a)
|
||||
expectType<Number>(setup.setupA)
|
||||
expectType<number | undefined>(setup.setupD)
|
||||
|
||||
// raw bindings props
|
||||
expectType<ExpectedProps['a']>(setup.setupProps.a)
|
||||
@ -239,6 +240,7 @@ describe('object props', () => {
|
||||
// instance
|
||||
const instance = new MyComponent()
|
||||
expectType<number>(instance.setupA)
|
||||
expectType<number | undefined>(instance.setupD)
|
||||
// @ts-expect-error
|
||||
instance.notExist
|
||||
})
|
||||
|
Loading…
Reference in New Issue
Block a user