Compile-time immutability with ComputedRef vs Readonly<Ref> types #6208
Unanswered
unshame
asked this question in
Help/Questions
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I've been trying to implement immutability-by-default for refs in a project I'm working on. I noticed that there are three different ways to mark a ref as readonly:
DeepReadonly<UnwrapNestedRefs<T>
returned by readonly whereDeepReadonly
is specific to@vue/reactivity
Readonly<T>
returned byshallowReadonly
whereReadonly
is the default ts helper typeComputedRef<T>
returned bycomputed
For refs with primitive values the first and the second options are the same, i.e.
I would expect
ComputedRef
to work exactly the same way, however it doesn't.This means that if I define a composable that accepts a ComputedRef, I will not be able to pass a regular Ref into it, which is not what I would expect. Instead, I have to define the argument as Readonly.
It seems the
ComputedRef
type exposes some vue internals. Digging through the commits, I found thatComputedRefSymbol
was added with script setup, whileeffect
was there from the beginning. This is not documented however, and the official docs state the types I expected:It seems like
ComputedRef
is an internal type that shouldn't be exposed, andReadonly<Ref>
should be used instead. Same goes for theWritableComputedRef
.So the question is: am I wrong in this assumption? Are there some use-cases outside of vue internals that require to differentiate between a computed and a regular ref, or to use the
effect
property? I feel like eithercomputed
needs to be changed to return aReadonly<Ref>
, or the documentation needs to be updated.Beta Was this translation helpful? Give feedback.
All reactions