Created
March 1, 2025 22:34
-
-
Save yazaldefilimone/f0490d73b6a103735942fdd86c6cac5a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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