Skip to content

Commit 82b28a5

Browse files
committed
fix(types): should unwrap array -> object -> ref
1 parent 028a8c2 commit 82b28a5

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

.eslintrc.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ module.exports = {
2626
overrides: [
2727
// tests, no restrictions (runs in Node / jest with jsdom)
2828
{
29-
files: ['**/__tests__/**'],
29+
files: ['**/__tests__/**', 'test-dts/**'],
3030
rules: {
3131
'no-restricted-globals': 'off',
3232
'no-restricted-syntax': 'off'

packages/reactivity/src/ref.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -165,10 +165,11 @@ type UnwrapRefSimple<T> = T extends
165165
| CollectionTypes
166166
| BaseTypes
167167
| Ref
168-
| Array<any>
169168
| RefUnwrapBailTypes[keyof RefUnwrapBailTypes]
170169
? T
171-
: T extends object ? UnwrappedObject<T> : T
170+
: T extends Array<any>
171+
? { [K in keyof T]: T[K] extends Ref ? T[K] : UnwrapRefSimple<T[K]> }
172+
: T extends object ? UnwrappedObject<T> : T
172173

173174
// Extract all known symbols from an object
174175
// when unwrapping Object the symbols are not `in keyof`, this should cover all the

test-dts/ref.test-d.ts

+17
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,23 @@ function plainType(arg: number | Ref<number>) {
4141
expectType<Ref<IteratorFoo | null | undefined>>(
4242
ref<IteratorFoo | null | undefined>()
4343
)
44+
45+
// should not unwrap ref inside arrays
46+
const arr = ref([1, new Map<string, any>(), ref('1')]).value
47+
const value = arr[0]
48+
if (isRef(value)) {
49+
expectType<Ref>(value)
50+
} else if (typeof value === 'number') {
51+
expectType<number>(value)
52+
} else {
53+
// should narrow down to Map type
54+
// and not contain any Ref type
55+
expectType<Map<string, any>>(value)
56+
}
57+
58+
// should still unwrap in objects nested in arrays
59+
const arr2 = ref([{ a: ref(1) }]).value
60+
expectType<number>(arr2[0].a)
4461
}
4562

4663
plainType(1)

0 commit comments

Comments
 (0)