TypeScript description
TypeScript (TS) is a programming language for elaborating modern web applications, providing more opportunities than the more common JavaScript. The TypeScript code is easily readable and reliable in comparison with its traditional counterpart and can also be compiled in it. The creation of this programming language began in 2012 at Microsoft. Even though it has a creator Andreas Heilsberg, the project continued its development as an open source. Immediately after its creation, the project gained popularity, and many JavaScript programmers began to relearn how to write TypeScript code. Nowadays, the usage of TypeScript is almost mandatory in most fintech-related solutions. Gain professional consultation to overcome the difficulties with your project from reputed TypeScript developers, it will speed up your work and guarantee the quality of code.
What are the advantages of TypeScript in comparison with JavaScript?
At first glance, the popularization of TS seems “strange” because there was a JS that coped with its task perfectly. However, let’s analyze their differences and see why the TS quote is justified.
- Firstly, TypeScript is based on strict typing. That is, the type of variables cannot be changed after the declaration. Although the code compiles in the same JavaScript, strict typing significantly reduces the cases of possible bugs that could be made when developing in JavaScript.
- Secondly, in TS, you can find elements peculiar mainly to object-oriented programming languages: inheritance and encapsulation.
- Thirdly, in TS, code is checked on the go, so it’s written faster and easier, so it is easy to scale, develop and maintain than its counterpart.
Is TypeScript worth it?
Of course, we have discussed many advantages of TS, but is TypeScript worth the effort?
- Firstly, TS detects 1 of the six most common bugs in the code. Programs consist of statements, and it is necessary to find out whether there are contradictions in these statements. Otherwise, such a program may not work during the compilation.
- Secondly, it is easier to read the TypeScript program due to the absence of code piles characteristic of JavaScript. Code written in JavaScript could turn into an infinite loop after some time.
- Thirdly, extensions of knowledge in TypeScript will allow you to better master and take a fresh look at JavaScript. After all, the whole range of features available on JS is also available on TS.
What are the disadvantages of TypeScript?
For all its attractiveness, this programming language is also not without disadvantages. Finding them turned out to be a difficult task.
- During development, we work with files *.ts, *.d.ts, *.map, *.js. For some of the newbies, there are too many additional files, which is very convenient when writing large projects, but it becomes inconvenient if your project is not that big.
- Now TypeScript is constantly updated and replenished; thanks to this, it becoming more strong and popular. However, if it loses Microsoft support, it will continue to exist only as an open source, which may lead to losing relevance.
How TypeScript works
Code written in TypeScript is not executed directly in all browsers. Therefore, TS — should not be considered an independent programming language but as a language add-on over JS. For it to work, an additional stage is necessary — transpilation, when the software converts the TS code into JS code.
When it comes to JS, you don’t need to install it in the system: browsers already support it. But TypeScript needs to be installed because the tsc module is necessary for translation. Install Node.JS and download the package to start programming in TypeScript through the NPM manager:
npm install -g typescript
After writing the code in TypeScript, an extension file .ts will be received. It must be compiled into JS using the tsc module. For this purpose, we need to write the console:
tsc file_example.ts.
Writing the first program.
Let’s look at how a simple code in TypeScript can be created.
Of course, everyone knows and loves the first code of all programmers “Hello, world!”. To write this code on our TS, we need only two lines:
let message: string = ‘Hello World’
console.log(message)
What is going on here? Let’s look at each line:
“let message: string = ‘Hello World’ ” = That is, we have created a variable “message” of type string and assigned it the value Hello world.
Additional information
In TypeScript, you can use a union to combine two types of data. For example, by writing: “let the union: null/string”. The union stores a value of type zero and string. There are seven types in total: number, string, boolean, void, null, and undefined. Furthermore, the TypeScript interface allows you to describe an object and the types of its fields.
Summary
TypeScript is now one of the most popular programming languages. Studying TS will not be superfluous, even if you base your knowledge on JS. After all, expanding knowledge in TS also improves your knowledge in JS. You can start your journey with this post, which compares JavaScript and Typescript in detail.