JavaScript and TypeScript Primitives
TypeScript improves JavaScript by adding static types — types that are checked before the code runs.
The most basic types in TypeScript are called primitives.
These types are the building blocks for the more complex types in your applications.
TypeScript includes all the JavaScript primitives, plus extra type features of its own.
Here are the five primitive types you will use most often.
Boolean
A boolean holds a true/false value.
Use it for flags, toggles and conditions.
Example
isActive is given the boolean type explicitly. hasPermission has no annotation, so TypeScript infers boolean from the value false.
boolean primitive type in two ways.
Declare isActive with an explicit boolean annotation set to true, and hasPermission set to false with no annotation, noting in a comment that TypeScript infers boolean. Nothing is printed.
Output: TypeScript file and captured output
let isActive: boolean = true;
let hasPermission = false; // TypeScript infers 'boolean' type
Number
A number holds both whole numbers and decimal numbers.
TypeScript uses the same number type for every numeric value, no matter how it is written.
Example
Decimal, hexadecimal, binary, octal and floating-point values all have the type number.
number type for every numeric literal form.
Declare five number variables: decimal (6), hex (0xf00d), binary (0b1010), octal (0o744) and float (3.14), each with a comment naming its notation. Nothing is printed.
Output: TypeScript file and captured output
let decimal: number = 6;
let hex: number = 0xf00d; // Hexadecimal
let binary: number = 0b1010; // Binary
let octal: number = 0o744; // Octal
let float: number = 3.14; // Floating point
String
A string holds text.
You can write strings with single quotes ('), double quotes (") or backticks (`). Backticks make template literals, which can embed values with ${...}.
Example
All three quote styles produce a string. The template literal in sentence inserts fullName and the result of age + 1.
string type with double quotes, single quotes and a template literal.
Declare color ("blue"), fullName ('John Doe'), a number variable age (30), and a string variable sentence built with a template literal that inserts fullName and age + 1. Nothing is printed.
Output: TypeScript file and captured output
let color: string = "blue";
let fullName: string = 'John Doe';
let age: number = 30;
let sentence: string = `Hello, my name is ${fullName} and I'll be ${age + 1} next year.`;
BigInt (ES2020+)
A bigint holds whole numbers larger than 2^53 - 1, which is the biggest whole number a number can represent exactly.
Example
BigInt(...) creates a bigint value, and TypeScript infers that type for hugeNumber.
bigint value for whole numbers larger than 2^53 - 1.
Declare a constant hugeNumber by calling BigInt(9007199254740991) and let TypeScript infer its type. Nothing is printed.
Output: TypeScript file and captured output
const hugeNumber = BigInt(9007199254740991);
Symbol
A symbol is a unique identifier. Two symbols are never equal, even if they were created with the same description.
Symbols are useful for unique property keys and constants.
Example
uniqueKey is a symbol used as a computed property key in obj. Reading obj[uniqueKey] prints the stored text.
symbol type used as a unique object key.
Create a constant uniqueKey typed as symbol from Symbol('description'), build an object obj whose computed key is uniqueKey with the value 'This is a unique property', then log obj[uniqueKey].
Output: TypeScript file and captured output
const uniqueKey: symbol = Symbol('description');
const obj = {
[uniqueKey]: 'This is a unique property'
};
console.log(obj[uniqueKey]); // "This is a unique property"