TypeScript

Want to understand a little more about TypeScript, this language that is so popular these days?

TypeScript is an open-source programming language developed by Microsoft. It was built on top of JavaScript, to take better advantage of its main features. In TypeScript, it was possible to add static typing capabilities that didn’t exist in JavaScript.

That is, you have all the JavaScript features in TypeScript plus several other features.

Although Typescript is a “superset” of JavaScript, when compiling the code, all TypeScript is converted/transpiled to Javascript.

Developers writing TypeScript code uses a simplified syntax that is clearer and supported by various TaskRunners or IDES, but your code will revert to JavaScript after transpiling. This happens because the browser does not understand the syntax of a programming language other than JavaScript.

TypeScript was considered by the public to be the fourth “most loved” language, according to the Stack Overflow survey in 2018. And it is among the fifteen most popular languages, according to the survey conducted by RedMonk.

Anders Hejlsberg, C# language architect and creator of Delphi and Turbo Pascal languages, worked on TypeScript development.

TypeScript can be used to develop JavaScript applications both on the client side and on the server side. There are several options available for transcompilation. The standard TypeScript Checker can be used, or you can use the Babel compiler to convert TypeScript to JavaScript.

Typescript X Javascript

The main differences between them are:

1 – TYPESCRIPT

– Static typing;
– Object orientation;
– Generics;
– Namespaces;
– Decorators.

2 – JAVASCRIPT

– Dynamic typing;
– Structured programming;
– Functions;
– Prototypes;
– Constructor functions.

By using the TypeScript “superset” we have better support for object-oriented programming, since JavaScript has been boosted with several possibilities that the pure language does not have.

In its simple form, JavaScript does not allow the use of interfaces in a clear way, for example, in addition to presenting a weak typing, which harms the development of applications at scale. In this way, TypeScript aims to circumvent these problems by adding a series of functionalities, which will be lost, since the final code becomes JavaScript when transpiled.

However, when programming, the developer will have greater possibilities and a simpler, clearer syntax, supported by all modern code editors.

As TypeScript is a modification of JavaScript, any file that has been written with the .js extension can be used within TS, as it accepts JS code by default. This is very positive, as it makes it possible to update systems built in JavaScript.

Key Advantages Of Typescript

There are two main advantages of TypeScript: static typing and object orientation.

Undoubtedly, the main characteristic of TypeScript is its strong typing, that’s why it has the name Type.

JavaScript, in most of its language design, is untyped and type inference only goes so far. Hence a greater need to use TypeScript if you want to overcome these deficiencies.

Here are some of the advantages of using TypeScript:

Encapsulation

Encapsulation can be understood as a way of structuring code so that certain blocks have access to specific points for the external environment. Thus, the visibility and accessibility of the internal elements of a class. That is, when using encapsulation, you can define which are the attributes of a class that will be visible to external users or that will be exposed to a public interface of the system.

Programmers who already use languages such as Java, PHP and C# are already used to declaring private attributes within their classes, guaranteeing this control. Example in Typescript:

     private _saldo: number;

Heritage

Inheritance is also a well-known principle, and based on it, a child class can inherit, or not, the behaviors and characteristics of a parent class, without having to redefine all the functions again.

In TypeScript the word used for inheritance is “extends” just like in Java. You must declare the new function by extending it from another, as in the example below:

     module Escola {
       class Pessoa { ...código aqui.. }
       class Aluno extends Pessoa { ...código aqui.. }
       class Professor extends Pessoa { ...código aqui.. }
     }

TypeScript makes it easy to write this inheritance-based code, which will later compile into pure JavaScript.

Polymorphism

We also have polymorphism, which allows objects to be used within the programming in different ways, according to the situation. That is, in a reverse-inheritance sense, we can assume that a parent class can use attributes from any of its child classes.

Abstraction

Abstraction can be considered the ability to highlight only some features of elements in the real world. It is widely used in object-oriented programming.

These characteristics are grouped in so-called classes, which represent parts of an element and its attributes for the solution of a given problem.

There are also “abstract classes”, which do not have a representation in the real world but, perform functions necessary for the system. These are called interfaces.

     export module Escola
     {
        export interface Nota { AlterarNota(nota: number); }
        export class Aluno implements Nota {
            AlterarNota(nota: number) { }
        }
     }

We can say that TypeScript is a potentiator of JavaScript and with it is possible to build complex systems without any problem and without being insufficient compared to other back-end languages, such as PHP or Java.

As we said, there are several advantages of using TypeScript and another one is being able to discover bugs during the implementation of your project, since it is possible to use the IDE’s Intellisense, which already visualizes improvement points and compilation problems.

The focus of Typescript is to include static typing in JavaScript, also including some features that facilitate the application of OOP concepts. However, it is through typing that we can build much more secure applications, greatly improving productivity.

Do you like our content? So, follow us on social media to stay on top of innovation and read our blog.

CONTENT

Our Latest Articles
Read about the latest trends in technology
Blog psychology
Psychology plays a crucial role in competitive programming. In addition to technical...
blog embedded
Embedded systems are devices dedicated to specific functions in various applications, such...
blog data tools
AI-powered data analytics tools like Databricks, Snowflake, and Google BigQuery are revolutionizing...

Extra, extra!

Assine nossa newsletter

Fique sempre atualizado com as novidades em tecnologia, transformação digital, mercado de trabalho e oportunidades de carreira

Would you like to speak with a Sales Representative?

Interested in: