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:
How to transpile typescript to javascript?
First we have to install the tsc compiler with this command, the -g flag is for global installation.
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
Once the compilation is finished all that remains is to run the js file with node.
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:
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.
It is also possible to create your own types:
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
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.
Main features
Data and conclusions