Skip to content

Commit

Permalink
Add type guards
Browse files Browse the repository at this point in the history
  • Loading branch information
bkiac committed Apr 15, 2024
1 parent 08a177c commit cb964ba
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/util.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import type {Ok, Err, Result} from ".";
import {ResultImpl, type Ok, type Err, type Result} from "./result";
import {OptionImpl, type Option} from "./option";
import {AsyncResult} from "./async_result";
import {AsyncOption} from "./async_option";

export type InferOk<T> = T extends Ok<infer O, any> ? O : never;

Expand All @@ -15,3 +18,19 @@ export type ExtractErr<T> = T extends Err<infer E, any>
: T extends Result<infer _, infer E>
? E
: never;

export function isResult<T, E>(value: unknown): value is Result<T, E> {
return value instanceof ResultImpl;
}

export function isAsyncResult<T, E>(value: unknown): value is AsyncResult<T, E> {
return value instanceof AsyncResult;
}

export function isOption<T>(value: unknown): value is Option<T> {
return value instanceof OptionImpl;
}

export function isAsyncOption<T>(value: unknown): value is AsyncOption<T> {
return value instanceof AsyncOption;
}

0 comments on commit cb964ba

Please sign in to comment.