<aside> šŸ’” Original Post by Martin Hochel: https://medium.com/@martin_hotell/10-typescript-pro-tips-patterns-with-or-without-react-5799488d6680

</aside>

šŸŽ’ this article uses following library versions:

{ "@types/react": "16.4.16", "@types/react-dom": "16.0.9", "typescript": "3.1.3", "react": "16.5.2", "react-dom": "16.5.2"}šŸŽ®Ā source code can be found on my github profile

TypeScript is definitely the best thing that happened to JavaScript. period.

Unfortunately, I cannot say the same about ā€œThe best thing that happened to Java/C# devs writing JavaScript with it šŸ‘€šŸ˜³šŸŒ€ā±ā€

Why 🤨?

Well, it definitely makes your Java/C# alter ego feel like home, having types within JavaScript (which is amazing !), but then, it introduces other ā€œlanguage featuresā€ which are not part of standard JavaScript, and because of those, it may throw a false prejudice about TypeScript, by putting it to a ā€œCompletely new languageā€ bag, which isn’t really true IMHO.

I’ve beenĀ always trying to stay away from various TS features (for a good reasons šŸ‘‰explained in this article) to stay in Idiomatic/Standard JavaScript land as much as possible.

This article describes various patterns/tips that I ā€œinvented/learnedā€ and have been using while using TypeScript and React for building UI’s.

NOTE:Initially, this blog post introduced ā€œonlyā€ 10 tips, During review of this post I already added 8 more šŸ’Ŗ. I may add additional ones in the future as React patterns/TS capabilities change/improve/evolve. Make sure to check this post time to time for any updates šŸ˜Ž

UPDATE:

26.1.2019Ā updated tip#9 šŸ‘‰Use type inference for defining Component State or DefaultProps23.1.2019Ā added tip#19 šŸ‘‰ Use type alias instead of interface for declaring Props/State#20 šŸ‘‰ Don’t useĀ FunctionComponent<P>Ā to define function component

Whole article is written like an ā€œstyle guideā€ with 3 sub-sections for every tip/pattern which consists of:

With that covered, let’s hop into 10++ TypeScript Pro tips/patterns with ( or without ) React.

1. Don’t useĀ publicĀ accessor within classes

Don’t: