Last active
February 12, 2023 02:38
-
-
Save ciiqr/c42d692fd943d40cfe4744fb8255e3f7 to your computer and use it in GitHub Desktop.
Revisions
-
ciiqr revised this gist
Feb 12, 2023 . 1 changed file with 12 additions and 0 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,12 @@ // 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; } -
ciiqr revised this gist
Jan 25, 2023 . 2 changed files with 2 additions and 2 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -40,7 +40,7 @@ export class Err<E extends Error> implements Result<never, E> { const idk = maybeIdk(); if (idk.isOk()) { if (idk.isOk()) { // Unnecessary conditional, value is always truthy. } console.log(idk); console.log(idk.value); 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 charactersOriginal file line number Diff line number Diff line change @@ -40,7 +40,7 @@ export class Err<E extends Error> implements Result<never, E> { const idk = maybeIdk(); if (idk.isOk()) { if (idk.isOk()) { // no warnings } console.log(idk); console.log(idk.value); -
ciiqr created this gist
Jan 25, 2023 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,61 @@ // 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()) { // } 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,61 @@ // 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()) { // } 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"); } 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1 @@ TODO: impl more functions, ie. same as: https://github.com/OliverBrotchie/optionals/blob/main/src/result.ts