home blog portfolio Ian Fisher

TypeScript cheatsheet

See also: ref/javascript

// array type
number[]

// type alias
type Coordinate = [number, number];

// function type
type Callback = (result: string) => string;

// algebraic data types
enum Marker {
  Variant1,
  Variant2,
}

interface Variant1 {
  marker: Marker.Variant1
  ...
}

interface Variant2 {
  marker: Marker.Variant2,
  ...
}

type MyType = Variant1 | Variant2;

tsconfig.json

{
  "compilerOptions": {
    "strict": true,
    "lib": ["ES6", "DOM"],
    "module": "NodeNext",
    "moduleResolution": "nodenext"
  },
  "include": ["./src/**/*.ts"]
}