iwantcoding.com
🔥 Daily 👥 Rooms 🏆 Top Log in Sign up

Built-in Types

Quick reference to the built-in types TypeScript provides out of the box — primitive, special, structural, and standard library.

Primitive types

TypeExample
string'hi', "hi", `tpl ${x}`
number42, 3.14
booleantrue, false
bigint9007n
symbolSymbol('id')
nullnull
undefinedundefined

Special types

TypeUse for
anyOpt-out — disables checks for this value.
unknown"I don't know yet — narrow before use."
neverFunctions that never return; bottom type.
voidReturns nothing useful.
objectAny non-primitive (rarely useful).

Structural shapes

TS
// Arrays
const xs: number[] = [];
const ys: Array<number> = [];
const ro: readonly number[] = [];
const ros: ReadonlyArray<number> = [];

// Tuples
const p: [number, number] = [3, 4];
const named: [x: number, y: number] = [3, 4];

// Records / dicts
const r: Record<string, number> = {};
const r2: { [key: string]: number } = {};

// Functions
type Fn = (a: number, b: number) => number;

Standard library types

TypeComes from
Array<T>, Map<K, V>, Set<T>, WeakMap<K, V>JS standard lib
Promise<T>, Awaited<T>Async / promises
Date, RegExp, ErrorJS classes
Iterable<T>, Iterator<T>, Generator<T>Iteration
JSON, Math, ObjectGlobal namespaces

DOM / browser types (with lib: ["DOM"])

HTMLElement, HTMLInputElement, Document, Window, Event, MouseEvent, KeyboardEvent, Response, Request, FormData, URL, URLSearchParams, ReadableStream, AbortController, … all available globally.

Node types (with @types/node)

Buffer, NodeJS.ProcessEnv, fs, path, http, … via import. process, global, require available as globals.

Tip: Open lib.es*.d.ts in node_modules/typescript/lib to see exactly what's available. It's surprisingly readable.

Example

Example
// Primitives:  string | number | boolean | bigint | symbol
// Special:     any | unknown | never | void
// Containers:  T[] | Array<T> | [a, b] | { k: V } | Record<K, V>
// Functions:   (a: T) => U
// Promises:    Promise<T>
// Sets/Maps:   Set<T> | Map<K, V> | WeakMap<K, V>
console.log('TS is a superset — every JS type is also a TS type');
Try it Yourself »

Exercise

Promise of a User is written…

<User>

Test yourself

Q1. Numeric primitive is…
Q2. Array of T can be written as…
Q3. Promise of T is written…

Discussion

Loading…