|
|
|
|
|
by laurencerowe
847 days ago
|
|
You can workaround the lack of exhaustive matching with the following pattern: type Variant = { kind: "value", value: string } | { kind: "error", error: string } | { kind: "unexpected" };
class Unreachable extends Error {
constructor(unexpected: never) {
super(`${unexpected}`);
}
}
function useVariant(variant: Variant) {
switch (variant.kind) {
case "value":
return variant.value;
case "error":
return variant.error;
default:
throw new Unreachable(variant);
}
}
The `new Unreachable(variant)` will fail the type check only when you have not exhaustively matched all variants. |
|