Skip to content

Type for T | Ref<T> #1366

Closed
Closed
@jods4

Description

@jods4

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:

// Typing
function useLength(input: string | Ref<string>): Ref<number>;

// Example uses
let s1 = "abc";
let l1 = useLength(s1);
let s2 = ref("123");
let l2 = useLength(s2);

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):

export type RValue<T> = T | Ref<T>

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>.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions