Image credit @ellladee
This week I have been working on a React app using Typescript and I've made a few discoveries that were very useful. This...
For further actions, you may consider blocking this person and/or reporting abuse
Some tips:
Regarding avoiding extends and just using union types: this is the kind of statement that definitely should be explained/quantified. Very easy for a beginner to see a comment like this and take it the wrong way.
The Record type is really cool! Definitely going to add that into the article. I'll have a look into the union types a bit, looks promising!
Some more tips:
Avoid using
any
at all costs! You will benefit from specifying types more precisely, and if you happen not to know the exact type, you can useunknown
type – which is similar toany
, but it doesn't allow code to escape the TypeScript type system. More on that: stackoverflow.com/questions/514398...Never-ever use
Function
andobject
types. It's the same thing as withany
: you lose all type safety this way. For your example it's better to useReact.EventHandler<T>
type and its specializations likeMouseEventHandler
,PointerEventHandler
and so on, or specify function signatures explicitly:If you want to dive deeper in type-safe TypeScript development, please check out my article (it's currently on Medium, but if an interest arise, I'll re-post it here): levelup.gitconnected.com/typesafe-...
Prefer using HTMLProps over HTMLAttributes:
stackoverflow.com/questions/481981...
Thanks for the advice!