Benefits of Implementing TypeScript Early
When you’re getting started on a new project, does it make sense to start off using TypeScript or to implement it later when it becomes more of a must-have? Let’s dive into when and why it’s better to start early.
Find errors earlier, with fewer bugs
- Make a change
- Go to the app and check the changed part and run the affected tests.
- Find out if anything broke.
With TypeScript, you can make the change and immediately be alerted by a compiler error message or IDE real-time feedback to any type errors. Of course, the TypeScript compiler won't resolve every problem and won't warn you about all bugs, but it helps speed things up.
The type information is also checked by the TypeScript compiler. The compiler will tell you when your code is not type-safe. It’s the classic benefit that type checking brings: Catching bugs earlier in the development process. You will catch and fix bugs during development instead of your customers finding them when your app is in production. The TypeScript compiler has a strict option that enables stronger type checking. With the strict option enabled, it can flag more potential problems.
It’s best to not use the strict mode when migrating an existing JS project to use TypeScript. In a TypeScript migration project, starting with the strict checks disabled makes it possible for you to have the build passing and the program running with less effort. If you enable the strict checks, you’ll need to do more significant refactoring such as defining the types to make the compiler happy.
It should be noted that this industry is using several techniques to catch bugs. Typescript and its static type checking are only one of these techniques. Some of the other recommended practices include Test Driven Development (TDD), pair programming, code reviews, design reviews, and linting.
If you are using TDD and code reviews, you are likely already efficient in finding bugs early. In this case, adding TypeScript will not have as significant of an impact on improving quality. You should consider your situation and try to determine whether the benefits of TypeScript will outweigh the costs of implementation.
Achieve better syntax completion in IDEs
If you are using an IDE like VSCode, you will get better syntax completion with TypeScript. Maybe it doesn't sound like a huge developer experience bump, but each little improvement potentially saves you time. Another plus: when you can define the type or interface once and not have to constantly remember the structure, you can focus on writing better business logic.
Imagine you’ve joined a project and have the task of adding a new feature, but that feature is connected with legacy code. TypeScript will help by alerting you when you’ve made a change and need to update the same piece somewhere else within the file.
Begin by setting a few configurations inside the
`"strict": false,` `"allowJs": true, // will allow using .js files without checking the type "skipLibCheck": true // will skip checking types in used libraries`
With those options, you can migrate from JS to TS file by file, simply changing the extension from
.ts(x), adding types in the files, and fixing any errors that pop up along the way.
Feel confident about the codebase
let query = '', you may later do something by mistake, such as
query = true, and it will be valid JS code.
When using TypeScript, you can't change the type of the variable, so if you make the
let query = '' variable, it will be
string type and you won't be able to change its type by mistake. If you want to let a variable be more than one type, you always do it explicitly using union type, for example,
string | number. TypeScript makes your code predictable and explicit.
Work as a team
It’s suitable for quick development. When working solo, It doesn’t matter if the code is hard to understand for others, as long as it makes sense to you. Most likely, you can understand it well enough to maintain it on your own.
The original developer was able to still make changes to it, but for me, the code did not make sense. The coding style, or the lack of it, made it impossible for me to understand.
Type declarations are part of what makes the code easier to understand. You can immediately see from the type declarations what data the function takes in. Interfaces describe the structure and typing for all complex parameters. There is no need to inspect the calling code, the function implementation, or the unit tests, to figure out what fields these complex objects are composed of.
A shared understanding of the code becomes vital when you are working as a team. Added type information brings better understanding.
- It is easier to understand
- It is faster to implement using modern IDEs
- It is quicker to refactor
- It gives better performance
TypeScript uses some of the most advanced type systems like Union Types, Intersection Types, Differentiating Types, Nullable Types, Conditional Types, and Type Inference.
In terms of language design, it is comparable to other modern languages like Kotlin, Go, or Rust.
Developers Love It
The TypeScript team at Microsoft has done an excellent job designing TypeScript as a pragmatic modern programming language. Their hard work is vindicated as the software development community loves TypeScript.
A language can have excellent features, but it does not mean that the language will be popular. Also, some languages gain popularity (like Scala and CoffeeScript) but quickly fade away.
With its enterprise-friendly features, which are very valuable in writing and maintaining a large code-base, more and more large companies are turning to TypeScript.
- Optional Chaining
- Nullish Coalescing
- Enum Type
- ECMAScript Private Fields
- Top-Level await
When don’t you want to implement TypeScript?
- No additional compiling step
- Concise and succinct code
- No need to learn the “extra” type system
- Easier to write higher-level abstraction without a type system
TypeScript does not come for free. There is some cost associated with using it:
- It takes effort to write type annotations and maintain them
- Type annotations add clutter to the code base
In the end, it comes down to your project and the amount of time and effort needed, and your team will have to weigh the pros and cons of implementation. If you decide to add TypeScript to your project, the benefits will appear quickly, from better code completion to bug prevention, and it will improve your teams’ lives when it comes to their code.