ref loses type when given a default value #11791
Unanswered
JammingBen
asked this question in
Help/Questions
Replies: 1 comment 2 replies
-
Kinda newbie here, but it seems like what you want is:
Because FooType and null are different types, so when you say bar is a FooType and then assign null, you've essentially lied to typescript, so it doesn't know what type it should be. Personally I like assigning the type on the variable declaration side of the equals sign: |
Beta Was this translation helpful? Give feedback.
2 replies
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 have the following code snippet:
Now
bar
is of typeRef<FooType>
:However, when I initialize
bar
with a default value, let's saynull
, TypeScript can't get theFooType
anymore:Can someone explain me why/what is happening here? I noticed that
const bar: Ref<FooType> = ref(null)
would still have the correct type. Is it recommended to use theRef
type when not working with primitives? I saw the recommendation in https://github.com/orgs/vuejs/discussions/9564#discussioncomment-7518938, but the discussion seems to be about generic types only.Beta Was this translation helpful? Give feedback.
All reactions