-
-
Notifications
You must be signed in to change notification settings - Fork 8.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Type for T | Ref<T> #1366
Comments
That's something I do quite a lot, as you said it can be done quite easily on userland. My only concern here is the naming |
I personally call them |
All my usages of MaybeRef (so far 1 xd) are actually getting Is there even a usecase when I would want to pass |
I'd say there surely are usecases even though they might not be that many. But since |
Computed has is not about reading, it can be read/write, too. What you're saying is that a readonly ref might make more sense: A |
There's always corner cases, but then one can use something else than |
Anyway you're right, I had readonly in mind, if you think that
+1 |
What I meant is that -- as far as concepts go -- you can create writable computeds: It looks like the type Makes sense since a writable computed is exactly like No matter what the actual types are, I think the goal is this:
Plus plain |
If we agree on MaybeRef being readonly then it should be added to the above requirements. I'm not sure if it's a bug in vue typings or limitations of TS, but you can pass a readonly ref (or computed ref) to a function that accepts writable refs: function t (x: Ref<number>) {
x.value = 1;
}
t(computed(() => 1)); // should not be allowed |
Evan has opened a PR that, among other things, adds |
@LinusBorg I guess we can close this now as this is implemented: https://vuejs.org/api/utility-types.html#mayberef |
What problem does this feature solve?
This is a small quality-of-life improvement.
Many libs and composables will accept potentially reactive inputs.
They will typically read those input by doing
unref(x)
, which works on both plain values and refs.So the following typing might become a common pattern:
What does the proposed API look like?
I think it would be convenient and set a common standard for everyone if Vue exported the following type (name up for bikeshedding):
Of course, everybody is free to declare such a type in userland.
I think it might be convenient to import it from "vue", which is an existing import in the source files; and it would give a common name to such inputs that every lib can use (familiarity for users).
It will also encourage library authors to accept
RValue<T>
inputs, rather than say,Ref<T>
.The text was updated successfully, but these errors were encountered: