Narrow Types with Type Guards

Type guards tell TypeScript what the value really is.

function printValue(value: string | number) {
  if (typeof value === "string") {
    console.log(value.toUpperCase());
  } else {
    console.log(value.toFixed(2));
  }
}

More on TypeScript

Recent Tips