Skip to content

Commit 536f623

Browse files
authoredAug 7, 2024··
fix(types/ref): correct type inference for nested refs (#11536)
close #11532 close #11537
1 parent 139548e commit 536f623

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed
 

‎packages/dts-test/ref.test-d.ts

+5
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,11 @@ describe('allow getter and setter types to be unrelated', <T>() => {
180180
const d = {} as T
181181
const e = ref(d)
182182
e.value = d
183+
184+
const f = ref(ref(0))
185+
expectType<number>(f.value)
186+
// @ts-expect-error
187+
f.value = ref(1)
183188
})
184189

185190
// shallowRef

‎packages/reactivity/src/ref.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,9 @@ export function isRef(r: any): r is Ref {
109109
* @param value - The object to wrap in the ref.
110110
* @see {@link https://vuejs.org/api/reactivity-core.html#ref}
111111
*/
112-
export function ref<T>(value: T): Ref<UnwrapRef<T>, UnwrapRef<T> | T>
112+
export function ref<T>(
113+
value: T,
114+
): [T] extends [Ref] ? IfAny<T, Ref<T>, T> : Ref<UnwrapRef<T>, UnwrapRef<T> | T>
113115
export function ref<T = any>(): Ref<T | undefined>
114116
export function ref(value?: unknown) {
115117
return createRef(value, false)

0 commit comments

Comments
 (0)
Please sign in to comment.