TypeScript

TypeScript

What is Typescript?

Typescript is a programming language that is transpiled into Javascript. It is a superset of Javascript, everything that can be done in Javascript can be done in Typescript, but Typescript has more functionality than Javascript.

Who created Typescript?

Typescript was created by Anders Hejlsberg in 2010 at Microsoft and its first version was available to the public in 2012, known as Typescript 0.8, the original idea was to replace javascript but as this would be impossible they decided to extend it, and improve the things where javascript is weaker and made this new syntax optional. Thus Typescript was born, a language that adds a lot of extra functionality to help developers and then transpiles it into javascript.

Shortly after its release, the language was praised by Miguel de Icaza, the Mexican programmer, who also criticised the lack of IDE support for Typescript, the only one that had support was Microsoft Visual Studio, which was not available on many operating systems.

This problem of lack of IDE support was solved and, in 2013, TypeScript support was present in several IDEs, such as Eclipse, Sublime, Vim and Emacs.

Why should you use TypeScript?

JavaScript is a loosely typed language, it can be difficult to understand what data types are passed, parameters and function variables have no information, so developers must look at the documentation or guess based on the implementation. TypeScript allows you to specify the supported data types within the code and has the ability to report errors when the types do not match, a very clear example is that TypeScript will report an error when passing a string to a function that expects a number:

No hay texto alternativo para esta imagen

How to transpile typescript to javascript?

First we have to install the tsc compiler with this command, the -g flag is for global installation.

No hay texto alternativo para esta imagen

Then we have to select the file we want to compile and type the following command, this will generate a js file which will be the ts code transpiled to js

No hay texto alternativo para esta imagen

Once the compilation is finished all that remains is to run the js file with node.

No hay texto alternativo para esta imagen

Typescript typing

Typescript is a typed language in which the data types of the variables are required, the data types are strict and must be respected.

existing data types:

- Boolean

- String

- Array

- Tuple

- Enum

- Any

- Void

- Null and Undefined

- Never: Return an exception or an error.

- Object

Example of variable typing:

No hay texto alternativo para esta imagen

Example of a possible error, in this case we type a variable to be a string and then try to reassign a number, this gives error because it only supports strings.

No hay texto alternativo para esta imagen

It is also possible to create your own types:

No hay texto alternativo para esta imagen

In this case I create my type name in which I define it as a string type, so it works as an alias to string.

Interfaces

An interface is a structure that defines a contract to be followed. Classes derived from an interface must follow the structure provided by its interface.

The TypeScript compiler does not convert the interface to JavaScript. It uses the interface for type checking. This is also known as "duck typing" or "structural subtyping".?

It is defined using the reserved word interface followed by the interface name with the first letter capitalised. And we place inside the braces the properties and methods with their corresponding typing

No hay texto alternativo para esta imagen




Classes

TypeScript classes are similar to ES6 classes, but TypeScript also has public/protected/private modifiers, which do not exist in ES6 classes. Also, in TypeScript you will have advantages because of the typing.

No hay texto alternativo para esta imagen

Main features

  • Strong typing: adds variable types, like in Java or C#.
  • Debugging: allows you to find bugs in the code under development and not at runtime, meaning that you don't need to run the code to realise that something is wrong.
  • Object-oriented language: TypeScript has all the tools of an object-oriented programming language like classes, interfaces, inheritance, modules, etc.
  • Ease of use: it is easy to use if you know JavaScript

Data and conclusions

  • TypeScript is a superset of JavaScript developed and maintained by Microsoft.
  • Its main features include: strong typing, ease of debugging, use of interfaces and classes.
  • TypeScript is used on both the frontend and backend.
  • TypeScript is not faster than JavaScript.
  • TypeScript is not complex to learn.
  • typescript works at compile time and not at run time.

要查看或添加评论,请登录

Valentin Repetto的更多文章

  • Bun 1.0

    Bun 1.0

    La reciente llegada de Bun 1.0 ha causado revuelo en la comunidad de desarrolladores JavaScript.

  • Nest js

    Nest js

    Introduction I have been hearing about nest js for a long time now, about its potential and features but I had never…

  • What happens when you type google.com in your browser and press Enter

    What happens when you type google.com in your browser and press Enter

    Do you know what happens when you search for google.com in the browser? Internet is currently available to everyone…

  • Recursion

    Recursion

    The process in which a function calls itself directly or indirectly is called recursion and the corresponding function…

  • Everything is an object in python

    Everything is an object in python

    Introduction Objects can be defined simply as the instance of a class that contains both data members and method…

  • Dynamic libraries

    Dynamic libraries

    To start talking about dynamic libraries, we will remember what libraries are and what they are used for. Certain types…

  • What happens when you type `ls -l *.c` in the shell?

    What happens when you type `ls -l *.c` in the shell?

    INTRODUCTION In this occasion, and as part of the “simple_shell” project, we have to write about the “ls -l *.c”…

  • Static libraries in c

    Static libraries in c

    This time I will inform you about the static libraries in c and how they work Whats are libreries? Libraries are a…

  • COMPILATION PROCESS

    COMPILATION PROCESS

    I will give a brief summary of how the compilation process works, from a high level language (that we can understand)…

社区洞察

其他会员也浏览了