Using basic TypeScript types
In this section, we’ll start by understanding how TypeScript types can be declared and how they are inferred from assigned values. We will then learn the basic types commonly used in TypeScript that aren’t available in JavaScript and understand helpful use cases.
Using type annotations
TypeScript type annotations enable variables to be declared with specific types. These allow the TypeScript compiler to check that the code adheres to these types. In short, type annotations allow TypeScript to catch bugs where our code uses the wrong type much earlier than we would if we were writing our code in JavaScript.
Open the TypeScript Playground at https://www.typescriptlang.org/play and carry out the following steps to explore type annotations:
- Remove any existing code in the left-hand pane and enter the following variable declaration:
let unitPrice: number;
The type annotation comes after the variable declaration. It starts...