RecipesChapter 2: Basic Types

Working with any and unknown

Recipe 2.2 from The TypeScript Cookbook

function doSomething(value: unknown) {
  if (typeof value === "string") {
    // value: string
    console.log("It's a string", value.toUpperCase());
  } else if (typeof value === "number") {
    // value: number
    console.log("it's a number", value * 2);
  }
}
Open in TypeScript Playground →