Skip to content

ref with generic type #9564

Nov 7, 2023 · 2 comments · 2 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: 2 comments 2 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
3 participants