Understanding the basic TypeScript/JavaScript features
Now that you understand the relationship between TypeScript and JavaScript, it is time to describe the basic language features you will need to follow the examples in this book. This is not a comprehensive guide to either TypeScript or JavaScript, but it should be enough to get you started.
Defining variables and constants
The let
keyword is used to define variables, and the const
keyword is used to define a constant value that will not change, as shown in Listing 3.15.
Listing 3.15: Defining variables and constants in the index.ts file in the primer folder
let condition = true;
let person = "Bob";
const age = 40;
The TypeScript compiler infers the type of each variable or constant from the value it is assigned and will generate an error if a value of a different type is assigned. Types can be specified explicitly, as shown in Listing 3.16.
Listing 3.16. Specifying types in the index.ts file...