Skip to content

rafatiburciorst/Either-Patern-Typescript

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Typescript Error Handling

type Either<L, R> = Left<L> | Right<R>;

class Left<L> {
  readonly value: L;

  constructor(value: L) {
    this.value = value;
  }

  isLeft(): this is Left<L> {
    return true;
  }

  isRight(): this is Right<never> {
    return false;
  }
}

class Right<R> {
  readonly value: R;

  constructor(value: R) {
    this.value = value;
  }

  isLeft(): this is Left<never> {
    return false;
  }

  isRight(): this is Right<R> {
    return true;
  }
}

function divide(a: number, b: number): Either<string, number> {
  if (b === 0) {
    return new Left("Cannot divide by zero");
  }

  return new Right(a / b);
}

const result = divide(10, 2);
if (result.isLeft()) {
  console.error(result.value); // Handle the error case
} else {
  console.log(result.value); // Handle the success case
}

About

Either Patern Typescript using typescript

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published