TODO: impl more functions, ie. same as: https://github.com/OliverBrotchie/optionals/blob/main/src/result.ts
Last active
February 12, 2023 02:38
-
-
Save ciiqr/c42d692fd943d40cfe4744fb8255e3f7 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
// preferred implementation | |
export type Result<T, E extends Error> = | |
| { readonly ok: false; readonly error: E } | |
| { readonly ok: true; readonly value: T }; | |
export function Ok<T>(value: T) { | |
return { ok: true, value } as const; | |
} | |
export function Err<E extends Error>(error: E) { | |
return { ok: false, error } as const; | |
} |
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
// explicit-return | |
export interface Result<T, E extends Error> { | |
isOk: () => this is Ok<T>; | |
isErr: () => this is Err<E>; | |
unwrap: () => T; | |
} | |
export class Ok<T> implements Result<T, never> { | |
constructor(readonly value: T) {} | |
isOk() { | |
return true as const; | |
} | |
isErr() { | |
return false as const; | |
} | |
unwrap() { | |
return this.value; | |
} | |
} | |
export class Err<E extends Error> implements Result<never, E> { | |
constructor(public error: E) {} | |
isOk() { | |
return false as const; | |
} | |
isErr() { | |
return true as const; | |
} | |
unwrap(): never { | |
throw new Error("unwrap...."); | |
} | |
} | |
const idk = maybeIdk(); | |
if (idk.isOk()) { | |
if (idk.isOk()) { | |
// Unnecessary conditional, value is always truthy. | |
} | |
console.log(idk); | |
console.log(idk.value); | |
} | |
const idk2 = maybeIdk(true); | |
if (idk2.isErr()) { | |
console.error(idk2); | |
console.error(idk2.error); | |
} | |
export function maybeIdk(beep?: boolean): Result<string, Error> { | |
if (beep) { | |
return new Err(new Error("beep")); | |
} | |
return new Ok("boop"); | |
} |
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
// implicit-return | |
export interface Result<T, E extends Error> { | |
isOk: () => this is Ok<T>; | |
isErr: () => this is Err<E>; | |
unwrap: () => T; | |
} | |
export class Ok<T> implements Result<T, never> { | |
constructor(readonly value: T) {} | |
isOk(): this is Ok<T> { | |
return true; | |
} | |
isErr(): this is never { | |
return false; | |
} | |
unwrap() { | |
return this.value; | |
} | |
} | |
export class Err<E extends Error> implements Result<never, E> { | |
constructor(public error: E) {} | |
isOk(): this is never { | |
return false; | |
} | |
isErr(): this is Err<E> { | |
return true; | |
} | |
unwrap(): never { | |
throw new Error("unwrap...."); | |
} | |
} | |
const idk = maybeIdk(); | |
if (idk.isOk()) { | |
if (idk.isOk()) { | |
// no warnings | |
} | |
console.log(idk); | |
console.log(idk.value); | |
} | |
const idk2 = maybeIdk(true); | |
if (idk2.isErr()) { | |
console.error(idk2); | |
console.error(idk2.error); | |
} | |
export function maybeIdk(beep?: boolean) { | |
if (beep) { | |
return new Err(new Error("beep")); | |
} | |
return new Ok("boop"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment