types(reactivity): handle primitive + object intersection types in UnwrapRef (#614)
This commit is contained in:
parent
2f6ec45d10
commit
2b4d0d6501
@ -85,6 +85,11 @@ function toProxyRef<T extends object, K extends keyof T>(
|
|||||||
|
|
||||||
type UnwrapArray<T> = { [P in keyof T]: UnwrapRef<T[P]> }
|
type UnwrapArray<T> = { [P in keyof T]: UnwrapRef<T[P]> }
|
||||||
|
|
||||||
|
// corner case when use narrows type
|
||||||
|
// Ex. type RelativePath = string & { __brand: unknown }
|
||||||
|
// RelativePath extends object -> true
|
||||||
|
type BaseTypes = string | number | boolean
|
||||||
|
|
||||||
// Recursively unwraps nested value bindings.
|
// Recursively unwraps nested value bindings.
|
||||||
export type UnwrapRef<T> = {
|
export type UnwrapRef<T> = {
|
||||||
cRef: T extends ComputedRef<infer V> ? UnwrapRef<V> : T
|
cRef: T extends ComputedRef<infer V> ? UnwrapRef<V> : T
|
||||||
@ -97,6 +102,6 @@ export type UnwrapRef<T> = {
|
|||||||
? 'ref'
|
? 'ref'
|
||||||
: T extends Array<any>
|
: T extends Array<any>
|
||||||
? 'array'
|
? 'array'
|
||||||
: T extends Function | CollectionTypes
|
: T extends Function | CollectionTypes | BaseTypes
|
||||||
? 'ref' // bail out on types that shouldn't be unwrapped
|
? 'ref' // bail out on types that shouldn't be unwrapped
|
||||||
: T extends object ? 'object' : 'ref']
|
: T extends object ? 'object' : 'ref']
|
||||||
|
@ -12,6 +12,8 @@ describe('with object props', () => {
|
|||||||
ddd: string[]
|
ddd: string[]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type GT = string & { __brand: unknown }
|
||||||
|
|
||||||
const MyComponent = defineComponent({
|
const MyComponent = defineComponent({
|
||||||
props: {
|
props: {
|
||||||
a: Number,
|
a: Number,
|
||||||
@ -57,6 +59,9 @@ describe('with object props', () => {
|
|||||||
c: ref(1),
|
c: ref(1),
|
||||||
d: {
|
d: {
|
||||||
e: ref('hi')
|
e: ref('hi')
|
||||||
|
},
|
||||||
|
f: {
|
||||||
|
g: ref('hello' as GT)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@ -88,6 +93,7 @@ describe('with object props', () => {
|
|||||||
// assert setup context unwrapping
|
// assert setup context unwrapping
|
||||||
expectType<number>(this.c)
|
expectType<number>(this.c)
|
||||||
expectType<string>(this.d.e)
|
expectType<string>(this.d.e)
|
||||||
|
expectType<GT>(this.f.g)
|
||||||
|
|
||||||
// setup context properties should be mutable
|
// setup context properties should be mutable
|
||||||
this.c = 2
|
this.c = 2
|
||||||
|
Loading…
x
Reference in New Issue
Block a user