In this post, you will learn how different ways declaring a ref with useRef hook influence the immutability of the current ref property. We will be...
For further actions, you may consider blocking this person and/or reporting abuse
Nice 😊II like reading about these kinds of nuances 👌
Thank you ! A question : your constants are declared as any. Shouldn't we type every declaration in Typescript ?
Hey, thank you for reaching out.
In TypeScript, you can leverage type inference. So, while I could explicitly annotate every variable with the corresponding type, I defer that work until necessary and rely on the notion of type inference.
You can read more about type inference here: typescriptlang.org/docs/handbook/t...
Yep, but it may be a better practice to explicitly type your constants while declaring them, especially when inferred type is not basic. You may gain time on weird issues later. And your code may be clearer when debugging 🙂.
I think explicitly typing everything makes it harder to read and now you have to understand the nuances of when you should or should not explicitly type.
Not only is the second one harder to read and parse, it actually widened our type.
This is a really simple assignation example and I agree with you on this (except for a little typo) . But for function returns, and libs specific types, making a rule of typing explicitly everything WILL help.
Example:
or
By explicitly typing, you give explicitly the mutability information to other developpers (or to you in a month or two). So you improve readability.
And if you try
Here, typescript tell you instantly you're making a mistake: "No, it's not mutable!".
I know there are many sources that says you can use implicit types, but if you use them too much, you may lose some typescript gifts.
I'd probably argue there should be a
useMutableRef
anduseRef
rather than complicated types to communicate intent. I often have these small functions that map to normal functions to more clearly communicate intent:It is even possible to create nice utility functions that make element refs easier to work with:
Notice the theme where the Typescript types start to disappear for normal usage? This means you can still get the benefits of Typescript without explicitly using Typescript. Even JavaScript users of your code can benefit. This technique works better for libraries, especially if you have JavaScript users of your library. You can use JSDoc to explicitly type JS code, but that is a pain for non-primitive types.
I say there doesn't need to be a tradeoff between Typescript gifts and expressing intent. If your team only uses Typescript and understands all the types in use, maybe you don't need to spend any extra time communicating intent through functions. But it is very useful for JavaScript users in addition to Typescript users who don't spend time finding out all the nuances of Typescript type differences like
useRef
. You have to learn something extra either way (type differences or which function to use), but why not communicate intent explicitly through names vs types?Because in this example case Typescript may :
I actually never type anything in TypeScript unless I have to, and I consider explicit types to be an antipattern.
In my opinion, it's easy to check VSCode's Intellisense to make sure that the right type was inferred.
In React, for example, I've never had to actually use the FC type or explicitly return JSX.Element; if I write a function component, then TypeScript catches it 100% of the time.
There are definitely certain cases where I type function returns, such as if I'm using a "pseudo enum" (union type of strings) and want to coerce the function return down from string to either "thingOne" | "thingTwo" -- so I do see your point.
Overall, I don't think it's useful for productivity or type safety to explicitly type things when the implicit type was correct, so I try to avoid it.
I did mutate an object that was created using
useRef
and initialized it withnull
like this:Then, mutate it like below:
The only thing happening here is that TypeScript will throw error since we are mutating a 'read-only' property but if you ignore this error, it does mutate it anyway.
Thanks for the explanation!
Thank you!! This kept tripping me up every time.