Created
November 19, 2014 14:11
-
-
Save s-panferov/5269524dcf23dad9a1ef to your computer and use it in GitHub Desktop.
Result type in TypeScript
This file contains 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
interface Result<T, E> { | |
map<U>(fn: (a: T) => U): Result<U, E>; | |
mapErr<U>(fn: (a: E) => U): Result<T, U>; | |
isOk(): boolean; | |
isErr(): boolean; | |
ok(): Option<T>; | |
err(): Option<E>; | |
and<U>(res: Result<U,E>): Result<U,E>; | |
andThen<U>(op: (T) => Result<U,E>): Result<U,E>; | |
or(res: Result<T,E>): Result<T,E>; | |
orElse<U>(op: (T) => Result<T,U>): Result<T,U>; | |
unwrap(): T; | |
unwrapOr(optb: T): T; | |
unwrapOrElse(op: (E) => T): T | |
} | |
class Ok<T, E> implements Result<T, E> { | |
private value: T; | |
constructor(v: T) { | |
this.value = v; | |
} | |
map <U>(fn: (a: T) => U): Result<U, E> { | |
return new Ok<U, E>(fn(this.value)) | |
} | |
mapErr <U>(fn: (a: E) => U): Result<T, U> { | |
return new Ok<T, U>(this.value); | |
} | |
isOk(): boolean { | |
return true; | |
} | |
isErr(): boolean { | |
return false; | |
} | |
ok(): Option<T> { | |
return new Some(this.value); | |
} | |
err(): Option<E> { | |
return None.instance<E>(); | |
} | |
and<U>(res: Result<U,E>): Result<U,E> { | |
return res; | |
} | |
andThen<U>(op: (T) => Result<U,E>): Result<U,E> { | |
return op(this.value); | |
} | |
or(res: Result<T,E>): Result<T,E> { | |
return this; | |
} | |
orElse<U>(op: (T) => Result<T,U>): Result<T,U> { | |
return new Ok<T,U>(this.value); | |
} | |
unwrapOr(optb: T): T { | |
return this.value; | |
} | |
unwrapOrElse(op: (E) => T): T { | |
return this.value; | |
} | |
unwrap(): T { | |
return this.value | |
} | |
toString(): string { | |
return "Some " + this.value; | |
} | |
} | |
class Err<T, E> implements Result<T, E> { | |
private error: E; | |
constructor(error: E) { | |
this.error = error; | |
} | |
map <U>(fn: (a: T) => U): Result<T, E> { | |
return this; | |
} | |
mapErr <U>(fn: (a: E) => U): Result<T, U> { | |
return new Err<T,U>(fn(this.error)); | |
} | |
isOk(): boolean { | |
return false; | |
} | |
isErr(): boolean { | |
return false; | |
} | |
ok(): Option<T> { | |
return None.instance<T>(); | |
} | |
err(): Option<E> { | |
return new Some(this.error); | |
} | |
and<U>(res: Result<U,E>): Result<U,E> { | |
return new Err<U,E>(this.error); | |
} | |
andThen<U>(op: (T) => Result<U,E>): Result<U,E> { | |
return new Err<U,E>(this.error); | |
} | |
or(res: Result<T,E>): Result<T,E> { | |
return res; | |
} | |
orElse<U>(op: (T) => Result<T,U>): Result<T,U> { | |
return op(this.error); | |
} | |
unwrapOr(optb: T): T { | |
return optb; | |
} | |
unwrapOrElse(op: (E) => T): T { | |
return op(this.error); | |
} | |
unwrap(): T { | |
throw "Err.get" | |
} | |
public toString(): string { | |
return "None"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@s-panferov