tsdjs / tsd

Check TypeScript type definitions
MIT License
2.4k stars 67 forks source link

Add support for expectNever #130

Closed SebastienGllmt closed 2 years ago

SebastienGllmt commented 2 years ago

Although this library supports expectType<never>(...), the expectType function returns void

This new expectNever function instead returns never.

This is great for asserting that all branches are being handled in your code

function handleFoo(foo: 1 | 2): boolean {
  if (foo === 1) {
    return true;
  }
  if (foo === 2) {
    return false;
  }
  expectNever(foo); // note: expectType<never>(foo) here would give an error
}

There is a library called assert-never that provides this functionality, but it's strange I would need a separate library to assert never vs. asserting other types