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.
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?
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.:
to more complicated and complex ones.
Kind of types in TypeScriptTypes 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:
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.
Let's talk about your business needs and prepare an action plan together
LET’S DO THIS