`satisfies` is my favorite TypeScript keyword

24 points by runxiyu


bhoot

My mental model is that x satisfies y checks that the type of x matches the type y wherever this check is placed.

Thus, satisfies also simplifies exhaustiveness check in a switch statement. What used to be:

switch(x) {
  // other cases

  default:
    const exhaustiveCheck: never = x;
    throw new Error(`Unknown case: ${exhaustiveCheck}`);    
}

now becomes:

switch(x) {
  // other cases

  default:
    // At this point, type of x is supposed to match the type `never`.
    // If it doesn't, then (x satisfies never) portion produces a compile-time error.
    // (throw x) portion ensures a runtime error too.
    throw x satisfies never;
}

Of course, this being TypeScript, there might be yet another better way already to check exhaustiveness. If so, then feel free to correct me!