In TypeScript, you can create an Either
type similar to Rust's Either
by using discriminated unions. This approach allows you to define a type that can hold one of two possible types (commonly referred to as Left
and Right
). Here's how you can implement it:
First, define the Either
type as a union of Left
and Right
interfaces. Each interface includes a discriminant property (tag
) to distinguish between the two variants.
// Either.ts
// Define the Left variant