Skip to content

ref with generic type #9564

Nov 7, 2023 · 3 comments · 3 replies
Discussion options

You must be logged in to vote

When working with a generic type, it's better to use the Ref type than to pass a type argument to ref<>(). This works fine:

const myRef: Ref<T | null> = ref(null);

myRef.value = props.foo;

playground

The reason that this works is that, because of ref unwrapping, there is a difference between what goes into a ref function and what it returns as it's value.

ref<T | null>()  //=> resulting type: Ref<UnwrapRef<T>>

Replies: 3 comments 3 replies

Comment options

You must be logged in to vote
1 reply
@hubdidub
Comment options

Comment options

You must be logged in to vote
1 reply
@hubdidub
Comment options

Answer selected by hubdidub
Comment options

You must be logged in to vote
1 reply
@bmulholland
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
5 participants