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); } }