Skip to content

Instantly share code, notes, and snippets.

@yazaldefilimone
Created March 1, 2025 22:34
Show Gist options
  • Save yazaldefilimone/f0490d73b6a103735942fdd86c6cac5a to your computer and use it in GitHub Desktop.
Save yazaldefilimone/f0490d73b6a103735942fdd86c6cac5a to your computer and use it in GitHub Desktop.
export type Either<L, R> = Left<L, R> | Right<L, R>;
export class Left<L, R> {
constructor(private readonly _value: L) {}
isLeft(): this is Left<L, R> {
return true;
}
isRight(): this is Right<L, R> {
return false;
}
unwrap(): L {
return this._value;
}
}
export class Right<L, R> {
constructor(private readonly _value: R) {}
isLeft(): this is Left<L, R> {
return false;
}
isRight(): this is Right<L, R> {
return true;
}
unwrap(): R {
return this._value;
}
}
export const left = <L, R>(l: L): Either<L, R> => new Left(l);
export const right = <L, R>(r: R): Either<L, R> => new Right(r);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment