Note that a variable can store values of different types throughout a program. It is not constrained to the data type of the initial value we give it at creation:
let a: any = "peanut";
console.log(typeof a); // Output: string
a = false;
console.log(typeof a); // Output: boolean
a = 13.7;
console.log(typeof a); // Output: number
With the any annotation the compiler lets the variable hold any type. Without it, TypeScript infers string from "peanut" and reports error TS2322: Type 'boolean' is not assignable to type 'string'. on the second assignment.
Assignment
Follow the Coding Tutorial and let's practice with data types!
Hint
Look at the examples above if you get stuck.
Free plan: 30 tutor messages. PRO is unlimited.
