-
Main types :
- string : used to store text values such as "student"
- number : used to store whole numbers and floating point numbers
- boolean : true or false
-
Less common primitives used in later versions of Javascript and TypeScript:
- bigint : stores whole and floating point values, allowing larger positive and neagtive values
- symbol : creates a globally unique identifier
- Explicit : the type is specified
let name : string = "Roland";- Implicit : TypeScript will infer the type based on the assigned value
let name = "Roland";When using implicit assignment, any future attemmpt to assign the same variable a value of a different type, an error will be thrown
let name = "Roland"; // inferred to type string
firstName = 20; // attempts to re-assign the value to a different typeTypeScript may not always properly infer what the type of a variable may be. In such cases, it will set the type to any which disables type checking.
// Implicit any as JSON.parse doesn't know what type of data it returns so it can be "any" thing...
const json = JSON.parse("25");
// Most expect json to be an object, but it can be a string or a number like this example
console.log(typeof json);