Why We Choose Typescript All the Way Through
Written by: Matias Hernandez
What can take them to the next level of confidence and productivity? Typescript can be that kind of addition that can leverage a world of new improvements in the developer experience, but it comes with a bit of a learning curve.
In this article, I will run you through some resources about Typescript, how this can give your team more confidence and increase productivity, and some examples about using Typescript with React.
Let's check some data about Typescript. In the results published in the report "State of Frontend 2020," there was a question, "Have you used Typescript during the last year," and an overwhelming 77% answered, Yes (45,000 developers responded).
There is another source of metric that you can check, the famous State of JS, where you can see that Typescript usage grows from 66% in 2019 to a gigantic 78% in 2020! Similar metrics for the Satisfaction question.
So, it’s easy to notice that Typescript is a big player in the web development world, but what is all this buzz about? What is Typescript?
What can Typescript do for me and my team?
- Typescript is a Test Typescript can be considered part of what is known as "statical analysis tools," which means that typescript processes verify your code without actually running, checking the data flow based on the types. Typescript compiler will parse your code, create an AST and then traverse the pieces to check how they communicate with each other. This process can be considered some type of test. The process checks your code to avoid bugs and avoid even committing to the main branch. A very constrained example is: Imagine you have a function that calculates your company's earnings, you expect that this function accepts numbers, right? But what happens if some unknown bug creates string data and passes that as an argument to your function? It will break, and you wouldn’t even notice until the tests are run or manually tested, or worse until it hits production. How could Typescript save you here?
Typescript will SCREAM to you that the arguments you are passing are strings and can't be used by your wonderful function by just giving the type of the expected data. And this will happen while you are writing the code! So using Typescript is like running a never-ending test that oversees what you write in order to ship clean code and not ship low-quality code.
- Real-time type checking Most IDE and code editors will run a type-check in the background reviewing the code you write and marking the type errors even before you save or run the code. This will help you to debug your code and be more confident about what was written.
Some rules of thumb
But typescript has good things, right? And to get most of it, we should keep some sort of standard way of doing things, so here are some typescript good practices or rules of thumb for your project.
- Use strict mode from the get-go. Typescript enables you to add types to your codebase progressively. Still, even though this is a solid point to help you with the transition, you should strive to use the full potential of typescript when you are working in a production environment. To get this power, you need to enable strict mode in your typescript configuration. This forces you to explicitly type everything and, in consequence, will improve your documentation and confidence in the code.
- Don’t use a non-null assertion operator.
This post-fix operator allows you to assert that a specific operator cannot be null nor undefined during runtime. Unfortunately, this means that by using it, you deliberately ignore an expression’s possible
- Use objects as functions arguments. Sometimes you have multiple arguments in your function, and type each of those arguments can be painful, so instead of separate input variables, you can use an object as a payload and kind the entire object.
- Choose unknown over anything else.
Sometimes you have to look for a more straightforward type, don’t fall into the temptation to use
anyas type; this breaks the type checking, so, prefer to use
unknowninstead, this type will force you to at least check the type before using the value.
Want to peek into our daily work? Our coaches recount real world situations shared as learning opportunities to build soft skills. We share frameworks, podcasts and thinking tools for sr software developers.
Keep on readingGo to Blog home
The (remote) opportunities
We expect professionalism and client service, so we can offer a deeply caring experience for our clients. In return, you get freedom to work wherever you want. No timesheets, no big brother watching every move. We trust you to know what’s best to find the right solution.