TypeScript

Types

Basic Types

  1. Primitive
type A = number
type B = string
type C = boolean
type D = undefined
type E = null
  1. Array
// single-type
type A = number[]
type B = Array<number>
 
// multi-types
type C = (number | string)[]
type D = Array<number | string>
  1. Object
type A = {
  x: number;
  y?: number; // Optional Property
}
  1. Function
type A = () => void
type B = (x: number) => number
type C = (x?: number) => number // Optional Parameter
type D = (x: number, ...rest: number[]) => void // Rest Parameters
  1. never
// empty set
function fn(x: number) {
    if (typeof x === 'number') { }
    else { } // x: never
}
 
// function never return
function fn(): never { // fn: () => never
    throw new Error()
}
  1. Any & Unkonwn
function fn(x: any) {
  x()   // ✅
  x.a() // ✅
}
 
function fn(x: unknown) {
  x()   // ❌
  x.a() // ❌
}

Collection Types

  1. Union
type A = 1 | 2 | 3
type B = number | string
  1. Intersection
type A = number | boolean
type B = number | string
type C = A & B // number

Type Inference

Variable Assignment

let A = 100 // number
let B = condition ? 100 : 'hi' // string | number
 
B = 100
console.log(B) // number
B = 'hi'
console.log(B) // string

Operator

  1. equality
function fn(x: number | null | undefined) {
    if (typeof x === 'number') { } // x: number
 
    if (x === null) { } // x: null
    if (x == null) { } // x: null | undefined
    if (x != null) { } // x: number
}
 
function fn(x: number | string, y: number | boolean) {
    if (x === y) { } // x: number
}
  1. in
type Dog = { name: string, bark: () => string }
type Cat = { name: string, meow: () => string }
 
function fn(x: Dog | Cat) {
  if ('name' in x) { } // x: Dog | Cat
  if ('bark' in x) { } // x: Dog
}
  1. instanceof
function fn(x: number[] | number) {
  if (x instanceof Array) { } // x: number[]
}

Control Flow

  1. if-else
function fn(x: number | string) {
  if (typeof x === 'number') {
    // x: number
  } else {
    // x: string
  }
}
  1. switch
function fn(x: number | string) {
    switch (typeof x) {
        case 'number': break // x: number
        case 'string': break // x: string
    }
}