In TypeScript, I'd like to define an object type that represents an inequality. Something like this:
{ "<": x => Math.sin(x) } // ok, y < sin(x)
{ ">": x => Math.sin(x) } // ok, y > sin(x)
{ ">=": -4, "<": 4 } // ok, -4 <= y < 4
But I want to prevent nonsensical combinations, like having both "less than" and "less than or equal to":
{ "<": 5, "<=": 5 } // should cause a TypeScript error
Is this possible? So far I've tried something that felt obvious: a union.
type Fn = number | ((x: number) => number)
type Inequality =
| Record<">", Fn>
| Record<"<", Fn>
| Record<"<=", Fn>
| Record<">=", Fn>
| Record<">" | "<", Fn>
| Record<">" | "<=", Fn>
| Record<">=" | "<", Fn>
| Record<">=" | "<=", Fn>
However, with this, TypeScript unions all these types together and allows any combination of these keys. Playground link.
Is it possible to make TypeScript only allow the combinations I declare?