TypeScript: Add to taste

Rajesh Naroth
2 min readMar 11, 2019

--

Photo credit https://www.pexels.com/@belart84

In the past couple of week, I’ve been researching into adopting TypeScript into our current and new React apps. The plan to add types everywhere was adding too much syntax noise and complexity. I agonized over how-to-type and to-type-or-not-to-type situations quite often. It didn’t feel right. As a proponent of minimalism and simplicity, I found myself in constant conflict with the language requirements.

And I finally did this in my tsconfig.json:

“strict”: false 😱

Suddenly, the sky cleared up and the sun was out again. The pressure was off. I didn’t have to add clutter where none was warranted. JavaScript still looked like JavaScript. Where I added types, the intention was clear and it was useful.

For new apps, using React hooks helped tremedously. The class construct was abandoned in favor of functional components. Goodbye clutter. Goodbye confusion.

Type everything is bad advice

You end up spending valuable engineering hours solving, debugging and code-reviewing type related concerns. I recently read an excellent article:

IMO, Typing is all about communication. Any other reasons such as VSCode integration, refactoring, documentation etc is secondary. If you have a shared library or module, type the parts to explain the interface.

JavaScript is a dynamically typed language. It is one aspect that I love about it. We don’t need to make JavaScript to be like Java or C++. Use its dynamic nature to your advantage. Power up the functional patterns.

Anything taken to the extreme can be detrimental. XML taken to the extreme gave us SOAP. J2EE to the extreme gave us EJBs. OOP taken to the extreme produced huge, rigid class hierarchies. Functional programming taken to the extreme will give you code that is incomprehensible. The same way, if you take TypeScript to the extreme, it will cost you.

The key is balance. Fanatic evangelism will build software that will quickly become legacy code that no one will want to touch or enhance. When adopting the shiny new thing, treat it like salt. Just add to taste.

If you would like to take an objective look at TypeScript, read this in depth article.

--

--

Rajesh Naroth
Rajesh Naroth

Written by Rajesh Naroth

Frontend Architect, San Jose, CA

No responses yet