3 ms·
Typescript is great but be aware that sometimes it can annoyingly slow you down. Consider these scenarios: - You want to use a third party library but it is co
by sureglymop 4y ago
Typescript is great but be aware that sometimes it can annoyingly slow you down. Consider these scenarios:
- You want to use a third party library but it is completely untyped
- You need an object but you're just prototyping it. You don't yet know which keys will be optional
- You type a key of one of your objects. Now you load or fetch data from an API and want to assign that to it. But typescript complains because the promise can also return undefined.
- and many more
Just be aware that it will annoy you at times.
- agos 4y agothird party libraries can be a pain even when completely typed, if their types rely on complex inference, are really complex, or make heavy use of subtyping
- jeroenhd 4y agoIf you don't know what parameters are optional, just add the little question mark to every variable and remove the ones that turn out mandatory? Better to have some type info than nothing at all. If you fetch data and TS tells you the result may be undefined then I guarantee you your code would've randomly crashed at some point in the future because of it. That error is a feature, not a bug!
- Dylan16807 4y agoYou can also toss on an exclamation point to insist it's not null, and come back later to clean those up.
- sureglymop 4y agoI am aware of those. I am just saying that it will slow you down or annoy you. For me, sometimes I am pretty much prototyping code just to see how something works. Once it does, I'll do it properly. Then I have to e.g. check if all keys of an object could be undefined before i do something with them.