20+ years web dev | Full-stack architect | AI integrator | Passionate about clean code, APIs, and docs | Building innovative SaaS with AI | Open source enthusiast
I cursed a lot when I started to use typescript as well. I felt as if someone tied rocks to my hands when I was able to write perfectly working JS before.
Soon you will adapt and naturally write code that gets along easier with TS. This is not exactly a drawback. Maybe it gets less elegant here and there, but its mostly for the better, trust me. Where you are, I have been - where I am, you will be :D
I don't have time to go into every single of your examples, but at least the first two:
TSC does a lot of code checking but it has its limits. It does not know that when you call localStorage.getItem(key), the key HAS to be present because its derived from the current keys in localStorage. To mitigate this, you can give the TSC a hint that a given value WILL be there by adding an exclamation mark: JSON.parse(localStorage.getItem(key)!)
TSC sees: "ah, he assigned a string here" and internally derives the type of "options" to {notation: string, maximumFractionDigits: number}. He is not exactly incorrect here. But string does not match the options wanted for NumberFormat. So what you need to do is:
As stated 2 times, I know this is mostly due to a lack of knowledge. So thanks a lot for the encouragements. I'm fully aware that a project with 80K stars and 33K commits over 650 contributors is not a failure.
I cursed a lot when I started to use typescript as well. I felt as if someone tied rocks to my hands when I was able to write perfectly working JS before.
Soon you will adapt and naturally write code that gets along easier with TS. This is not exactly a drawback. Maybe it gets less elegant here and there, but its mostly for the better, trust me. Where you are, I have been - where I am, you will be :D
I don't have time to go into every single of your examples, but at least the first two:
TSC does a lot of code checking but it has its limits. It does not know that when you call
localStorage.getItem(key), the key HAS to be present because its derived from the current keys in localStorage. To mitigate this, you can give the TSC a hint that a given value WILL be there by adding an exclamation mark:JSON.parse(localStorage.getItem(key)!)This is somewhat the same problem:
TSC sees: "ah, he assigned a string here" and internally derives the type of "options" to
{notation: string, maximumFractionDigits: number}. He is not exactly incorrect here. Butstringdoes not match the options wanted for NumberFormat. So what you need to do is:As stated 2 times, I know this is mostly due to a lack of knowledge. So thanks a lot for the encouragements. I'm fully aware that a project with 80K stars and 33K commits over 650 contributors is not a failure.
Also, thanks for the two advices. BTW, @ryands17 taught me that
Intl.NumberFormatOptionsis an existing type.An easier way to do this is
The "const assertion" will concrete the value to "compact" instead of string
Edit: oops, someone already mentioned that, sorry.
You should specify that
notationnot just thestringtype but concrete typeIntl.NumberFormatOptions['notation']