DEV Community

loading...

4 reasons why you won't regret using a type system

Babak
Twitter @babakness https://twitter.com/babakness
・2 min read

To type or not to type

The debate on the benefits and limitations of type systems is ongoing and there are legitimate points on both side. In this article, I will explore a few benefits I've personally experienced from using type systems.

  1. Refactoring - I'm always aiming to reduce complexity. It is a popular notion that it takes work express an idea concisely. So, when you're paying off technical debt, a type system can help tremendously. When the workings of some functionality or process is spread across many different files, libraries, and/or components, type systems are a boon.

  2. Composition - writing unit tests is great practice. A type system can go further and test how different units compose and come together. In the example below, all the functions are working fine; the problem lies in our use of them. We are accessing a property that doesn't exist--thankfully, this well typed pipeline function caught this right away.

    pipeline from soultrain library https://github.com/babakness/soultrain/blob/v0.0.43/src/function/pipeline.ts

  3. Runtime errors - it's better to have compile-time errors than runtime errors--especially if they're hard to catch!

  4. Collaborating - between colleagues, teammates, library authors, or yourself from the past or in the future, I've found that a type system helps me understand how data is transformed, how events are handled, and what parameters a component expects.

Yet, type systems are not necessarily the right solution all the time. One common scenario I find myself leaving them out is when I'm teaching. Other times include when I'm prototyping or kicking the tires on some new library. It makes sense to just sketch something out and see what happens.

That said, the way I see it, for projects that can grow in complexity, there are more benefits to using them than not. The debate will go on, and there are other valid points against them.

To paraphrase Kierkegaard, use a type system or don't use a type system; you may still regret it. Hopefully I've given four good reasons on why you should use them, without regret! πŸ˜‰

Discussion (2)

Collapse
josefina profile image
Josefina Terrera

To type or not to type, that is the question: whether ’tis nobler in the mind to suffer the slings and arrows of runtime errors, or to take arms against bugs in production, and via strictly typed, end them?

-great article Babak! I agree that typescript is a good base on scalable projects. It can be a good source of information for other people working on it.
But, as I'm learning something new, I tend to just use simple JS.

Collapse
itsjzt profile image
Saurabh Sharma

using typescript from start of the year, agree with your thoughts.

  • Using types takes some time which is paid as dividents later.

  • It makes refactor much easier