Why the type returned by function ref
is not equal to Ref
#11180
nieyuyao
started this conversation in
General Discussions
Replies: 2 comments 1 reply
-
I'd assume due to the value passed to const msg1 = ref('Hello World!')
const msg2 = ref(msg1) |
Beta Was this translation helpful? Give feedback.
0 replies
-
nested refs will be unwrapped, so the return type is different than the type you pass in. One example: const initial = {
x: ref('x'),
}
const myref = ref(initial)
// reading x from initial:
initial.x.value // x is a ref, so we need to use x.value to get the value of that ref
//reading x from the ref:
myref.value.x // no x.value needed, because the ref is unwrapped by Vue's reactivity
// so this is the shape of initial:
Interface Initial = {
x: Ref<string>
}
// and this is the shape of myref's value:
interface MyRefsValue {
x: string
}
// To get from one to the other, we use a type that does this nested unwrapping on the type level. |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
As shown in the doc, the type of
ref
andRef
are as follows,Ref<UnwrapRef<T>>
is not equal toRef<T>
. Some issues also seem to be related to this, such as #10476 and #10862 .On the other hand, if we don't pass a parameter to ref, the returned type is
Ref<T | undefined>
.Why the returned type of
ref
can't be written asRef<T>
but needs to be wrapped byUnwrapRef
.Beta Was this translation helpful? Give feedback.
All reactions