Typescript
What is
Basically, it's JavaScript with types. This simple idea may make it seem like TypeScript don't do that much, but don't fool yourself: it adds a lot of control over JavaScript to the code, avoiding errors that lack of types usually brings.
The greatest practicality that typescript brings is being able to move the cursor over a variable or function, and to know exactly its return, making it much easier to deduce its usability.
How to use
Basic types
- boolean
- number
- string
- array
- tuple
- enum
- any
The type
any
is usually assigned when the typescript cannot identify the typing, but can also be assigned manually. It is not recommended to leave the type asany
. Defining exactly the types helps not only typescript, but also the programmer to better understand the code.
Specific types
If your variable doesn't fit the basic types, you can create an interface where you configure your object.
If your variable comes from a package, Typescript may not be able to assign its type. In such cases, the package usually has a type library.
How to learn
Useful material
Validating knowledge
- A very simple way to validate knowledge in typescript, is to take a code in javascript and adapt it to typescript
Useful commands
npm install typescript --save-dev
- installs typescript via npm