types: fix ref unwrapping when nested inside arrays (#331)
This commit is contained in:
parent
58fcd15000
commit
3cd2f7e68e
@ -80,6 +80,25 @@ describe('reactivity/ref', () => {
|
|||||||
expect(typeof (c.value.b + 1)).toBe('number')
|
expect(typeof (c.value.b + 1)).toBe('number')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('should properly unwrap ref types nested inside arrays', () => {
|
||||||
|
const arr = ref([1, ref(1)]).value
|
||||||
|
// should unwrap to number[]
|
||||||
|
arr[0]++
|
||||||
|
arr[1]++
|
||||||
|
|
||||||
|
const arr2 = ref([1, new Map<string, any>(), ref('1')]).value
|
||||||
|
const value = arr2[0]
|
||||||
|
if (typeof value === 'string') {
|
||||||
|
value + 'foo'
|
||||||
|
} else if (typeof value === 'number') {
|
||||||
|
value + 1
|
||||||
|
} else {
|
||||||
|
// should narrow down to Map type
|
||||||
|
// and not contain any Ref type
|
||||||
|
value.has('foo')
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
test('isRef', () => {
|
test('isRef', () => {
|
||||||
expect(isRef(ref(1))).toBe(true)
|
expect(isRef(ref(1))).toBe(true)
|
||||||
expect(isRef(computed(() => 1))).toBe(true)
|
expect(isRef(computed(() => 1))).toBe(true)
|
||||||
|
@ -74,7 +74,6 @@ export type UnwrapRef<T> = {
|
|||||||
ref: T extends Ref<infer V> ? UnwrapRef<V> : T
|
ref: T extends Ref<infer V> ? UnwrapRef<V> : T
|
||||||
array: T extends Array<infer V> ? Array<UnwrapRef<V>> : T
|
array: T extends Array<infer V> ? Array<UnwrapRef<V>> : T
|
||||||
object: { [K in keyof T]: UnwrapRef<T[K]> }
|
object: { [K in keyof T]: UnwrapRef<T[K]> }
|
||||||
stop: T
|
|
||||||
}[T extends ComputedRef<any>
|
}[T extends ComputedRef<any>
|
||||||
? 'cRef'
|
? 'cRef'
|
||||||
: T extends Ref
|
: T extends Ref
|
||||||
@ -82,5 +81,5 @@ export type UnwrapRef<T> = {
|
|||||||
: T extends Array<any>
|
: T extends Array<any>
|
||||||
? 'array'
|
? 'array'
|
||||||
: T extends BailTypes
|
: T extends BailTypes
|
||||||
? 'stop' // bail out on types that shouldn't be unwrapped
|
? 'ref' // bail out on types that shouldn't be unwrapped
|
||||||
: T extends object ? 'object' : 'stop']
|
: T extends object ? 'object' : 'ref']
|
||||||
|
Loading…
Reference in New Issue
Block a user