upvote
Isn't that overkill? You can check exhaustiveness with a trivial little helper:

    function exhaustive(witness: never): never {
      return witness
    }
https://www.typescriptlang.org/play/?#code/FAFwngDgpgBAKgZQB...
reply
Agree, python calls this little helper assert_never[0]. I opened an PR to add it to the TypeScript docs some time ago: https://github.com/microsoft/TypeScript-Website/pull/3599

[0]: https://docs.python.org/3/library/typing.html#typing.assert_...

reply
deleted
reply
In the example you don't even need the default clause and the helper (the error message without it is worse though).
reply
you can just to

    default:
        matched satisfies never
        throw new Error(...)
You don't even need a function.
reply
I knew I should've written a disclaimer about doing this in vanilla TypeScript

Yes, you can do it via an assert never in a switch. That isn't pattern matching. That's an exhaustive switch enforced at compile time

Which is nice, but again, it's not pattern matching.

reply
You don't need a library for this; the idiomatic approach is to lean on TypeScript's narrowing and native control flow (ifs and switches, or maybe IIFEs), along with (maybe) the never type to enforce exhaustiveness.

https://www.typescriptlang.org/play/?jsx=4#code/JYWwDg9gTgLg...

I guess the library is slightly nicer, arguably, but I don't think it's worth the external dependency and added complexity.

reply
deleted
reply