TypeStrong/ts-expect

Exhaustive checks example

ThomWright opened this issue ยท 2 comments

If you add a return type to the function in the Exhaustive checks example, it no longer compiles:

type SupportedValue = "a" | "b"

function doSomething(value: SupportedValue): boolean {
  switch (value) {
    case "a":
      return true
    case "b":
      return true
    default:
      expectType<never>(value)
  }
}

In order for this to work, expectType would need to return never, as in this example.

Oh yeah, I should have actually tested the snippet ๐Ÿ˜„ I think the correct behavior in this snippet is to return false, and I can add an expectNever function that actually throws and returns never.

Great, thank you.

BTW, I've started sprinkling some type assertions around my codebase and it feels very natural. Really enjoying how much easier it makes it to sense-check my types.