To declare a variable in TypeScript, we have to specify the type.
let variableName : variableType = value;
Let’s take a look at a prior example:
The same variable declarations in TypeScript would be:
Now let’s write some broken code that will update the value of
distancetoMoon = "384401";
Arrays in TypeScript must contain values of the same type. When declaring an array, the type needs to be declared.
let arrayName: number = [10,9,8];
What if the array needs to hold values of different types?
Now, we need a tuple. A tuple is a special structure in TypeScript that can hold as many values as needed of different types.
let tupleName: [number, string, number]; tupleName = [10, "9", 8];
Check Your Understanding
- dynamically, dynamically
- statically, statically
- statically, dynamically
- dynamically, statically
The correct TypeScript declaration of the variable,
astronautName, that holds the value,
"Sally Ride", is:
let astronautName = "Sally Ride";
let astronautName = string: "Sally Ride";
let astronautName: string = "Sally Ride";
string astronautName = "Sally Ride";
What is the appropriate collection type for this collection,
["Costco", 4.97, 3]?