Open14

【英語ドキュメントから抜粋】エンジニア向け英語語彙集

SunnySunny

Narrowing - (Typescript用語) Union typeをif文などでふるいにかけて型を限定していくこと
Type guards - Narrowingのふるいとなるif文などのこと

例(Narrowing):https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#:~:text=we can use narrowing to check for values that might be null%3A

we can use narrowing to check for values that might be null:

例(Type guard):https://www.typescriptlang.org/docs/handbook/2/narrowing.html#:~:text=Within our if check%2C TypeScript sees typeof padding %3D%3D%3D "number" and understands that as a special form of code called a type guard.

Within our if check, TypeScript sees typeof padding === "number" and understands that as a special form of code called a type guard.

function doSomething(x: string | null) {
  // type guard
  if (x === null) {
    // do nothing
  } else {
    // x: string
    console.log("Hello, " + x.toUpperCase());
  }
}