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
Uh oh!
There was an error while loading. Please reload this page.
-
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 whereDeepReadonlyis specific to@vue/reactivityReadonly<T>returned byshallowReadonlywhereReadonlyis the default ts helper typeComputedRef<T>returned bycomputedFor refs with primitive values the first and the second options are the same, i.e.
I would expect
ComputedRefto 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
ComputedReftype exposes some vue internals. Digging through the commits, I found thatComputedRefSymbolwas added with script setup, whileeffectwas there from the beginning. This is not documented however, and the official docs state the types I expected:It seems like
ComputedRefis 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
effectproperty? I feel like eithercomputedneeds to be changed to return aReadonly<Ref>, or the documentation needs to be updated.Beta Was this translation helpful? Give feedback.
All reactions