Basic Types | Typescript

Simple types#

boolean / number / string / any / void / null / undefined / object / symbol / bigint

Array#

Array<T> | T[]

Tuple#

let x: [string, number];

Enum#

// Numeric enum
enum Color {Red, Green, Blue}
enum Color {Red = 1, Green, Blue}
enum Color {Red = 1, Green = 2, Blue = 4}
// A handy feature of enums is that you can also go from a numeric value to the name of that value in the enum
let colorName: string = Color[2]; // Green
// String enum
enum Color {Red = 'RED', Green = 'GREEN', Blue = 'BLUE'}
// Heterogeneous enums
// it's advised that you don't do this.
enum Color {Red = 1, Green = 'GREEN', Blue = 'BLUE'}

Never#

The never type represents the type of values that never occur.

For instance, never is the return type for a function expression or an arrow function expression that always throws an exception or one that never returns; Variables also acquire the type never when narrowed by any type guards that can never be true.

The never type is a subtype of, and assignable to, every type; however, no type is a subtype of, or assignable to, never (except never itself). Even any isn’t assignable to never.

// Function returning never must have unreachable end point
function error(message: string): never {
throw new Error(message);
}
// Inferred return type is never
function fail() {
return error("Something failed");
}
// Function returning never must have unreachable end point
function infiniteLoop(): never {
while (true) {
}
}

Unions and Intersections#

Union#

string | number

Intersection#

A & B