After years of working in Javascript, Typescript is was something on my “things to learn” list.
After working with Typescript for almost 1 year, here are my conclusions:
It’s not that scary. All Javascript is a valid Typescript
if you are planning to start a new project, do it with typescript. You don’t have to provide 100% types to every function and variable. You can gradually increase types coverage as you go. TypeScript can determine possible types there are for you.
Refactoring code is a breeze
Once you have set up your types, refactoring your code now takes less effort. You can even call it a pleasant experience. Compiler tsc
combined with IDE will automatically provides you with errors at specific places where you need to change your code according to types.
Less bugs.
Typescript only provides compile-time type safety. There is no type checking in runtime.
(checkout run-time type checking libraries like zod, ajv, yup, joi )
So if there is a bug in production, typescript will not help you. However it does prevent you from creating bugs while you are coding or refactoring. 10 minutes invested in typescript will potentially save you hours of debugging in the future.
Time-consuming ? Yes and No
You will definitely spend more than time developing in Typescript than plain Javascript.
However this time will pay off as you will create less bugs in production.
Replaces tests? Arguably
If you project does not have any test, typescript is the least thing you can do.
There is a good post by Shawn Wang you can read about this.
https://css-tricks.com/types-or-tests-why-not-both/
- Tests are best at ensuring happy paths work.
- Types are best at preventing unhappy paths from existing.
or this talk by Gary Bernhardt
https://www.destroyallsoftware.com/talks/boundaries
After using Typescript, typing vanilla javascript feels awkward. It seems like a naked and vulnerable code without any protection.
I guess you can’t go back.
Top comments (0)