Why it's worth to use TypeScript in 2021

TypeScript is a well-known programming language that is compiled into JavaScript. It is also called " superset of JavaScript ", which means that it contains all the functionalities of JavaScript , supplementing them with additional possibilities and functions.

Why it's worth to use TypeScript in 2021

It was created by Microsoft Corporation and was first made available in 2012. Its popularity has started to grow in recent years, and now, TypeScript is in the top 10 most used languages . Although often associated with animations on websites, it is also used on the server side, in mobile and web applications. Why is TypeScript also worth taking an interest in it, if you still don't use it in your work? Let's check it out!

Typescript - what are its advantages?

  • TypeScript helps reveal hidden errors , by returning them when, for example, there are too many or too few arguments.
  • It’s open to changes - every developer can submit comments and pull requests, due to the fact that the TS source code is available on GitHub. With the scale of stars it received, which is already over 73.4 thousand, it is a huge commitment behind the tool, and thus the possibility of influencing further tips from many thousands of users testing it.
  • Regularly updates - corrections are released on a regular basis, with major corrections or new constructs being made approximately every second of them. It is also much easier to support it, by a technology giant such as MC, which will probably take care of its valuable and regular updates for a long time to come.
  • It prevents the technological debt, by writing structured, transparent code .
  • It allows for rigid typing, that is typing variables during program compilation, in order to avoid accidental errors in parameter passing.

Sounds convincing? If we already know what the advantages of TS are, let's see where we can use it.

Where can the TypeScript be used?

TypeScript can be used wherever JavaScript is, due to the fact that it is compiled for it, i.e. as a result - both on the front and back of the created solutions. Code written in TypeScript , on the other hand, will be much easier to understand, than created in pure JavaScript for a person who is just entering a given project. Typed functions and variables allow you to easily find out what type of value we get and whether we should pass it to some function. IDEs are more helpful to the person writing code if the project is written in TypeScript . This solution can help you find errors and show you clues, that are difficult to identify by yourself.

The popular, appreciated configuration, is that if we write the backend of the project in TS and Node.js. This combination let you create a shared package between backend and frontend containing all the types. In this case when there are some changes in the API shape, you will see that immediately. Why? Because you and the backend devs share the same package. Therefore, you don’t have to worry that you miss important changes.

Such a solution is created using various kind of types, ranging from simple ones, f.e.:

  • boolean (true / false values),
  • number (it contains the numbers),
  • string (a string that conveys text.).

to more complicated and complex ones.

Kind of types in TypeScript

Types in TS can be divided into basic and complex. The basic ones include, among others, array, or the above-mentioned number and string. You can read more about the basic types here: https://www.typescriptlang.org/docs/handbook/basic-types.html

The most interesting types, however, are the complex ones, the most expressive, such as Void or Never. You can also combine types for best results, resulting in less standard builds.

One example is a function, that takes arguments in the form of inscription, which should belong to the set "yes" or "no". In this case, we can prepare the type, that will take "yes" or "no", and when typing an argument into a function, it will highlight the selectable options. If you enter a value outside the set, an incorrectness will be indicated in the argument provided.

type Decision = 'yes' | 'no'

const test = (decision: Decision): string => {
  switch (decision) {
    case 'yes':
      return 'tactic';
    case 'no':
      return 'non-compliance';
    default: 
      return '';
  }
}

You can also type on example the whole object:

{
  name: string,
  age: number,
  male: boolean,
  hobby: string [],
}

but this solution also allows us, to create an interface in which we mark each props taken by a given component or view:

interface ListProps {
  items : Item [];
  onExpand: () => void;
  onClose: () => void;
  header ?: string;
}

Here the Item may be what we define as the Item interface.

TypeScript - does it have flaws?

As in any technology, we can find shortcomings that, for some, are unnoticeable, for others, are discriminating disadvantages. Let's get to know a few of them:

  • Using TypeScript is risky - it builds habits of automatic checking that are not in other solutions. This can reduce your attention, when creating products in other technologies.
  • Using TS makes sense only when you use the types.
  • Typing in TypeScript only exists at the transpilation level and is irretrievably lost after this process. This means that if we create a library and someone uses it as JS code instead of TS , then ... typing will not help him at all.
  • Due to long initial preparations, developers often give it up.

Everything considered, you need to devote time to appropriate preparation at the initial stage of operation. For this reason, TypeScript is best for larger projects where recovery from errors can take much longer, than preparation time.

Looking for React or React Native talents?

Need help? Our team will help you straight away.