Skip to content

Commit

Permalink
feat: add InferOk and InferErr utility types for Result type inference
Browse files Browse the repository at this point in the history
  • Loading branch information
braden-w committed Dec 23, 2024
1 parent b4a0bc4 commit 043d624
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/spotty-rice-roll.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@epicenterhq/result": patch
---

Add InferOk and InferErr utility types for Result type inference
12 changes: 12 additions & 0 deletions src/result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,24 @@ export type Ok<T> = {
ok: true;
data: T;
};

export type Err<E> = {
ok: false;
error: E;
};

export type Result<T, E> = Ok<T> | Err<E>;

export type InferOk<R extends Result<unknown, unknown>> = R extends Ok<infer U>
? U
: never;

export type InferErr<R extends Result<unknown, unknown>> = R extends Err<
infer U
>
? U
: never;

export const Ok = <T>(data: T): Ok<T> => ({ ok: true, data });
export const Err = <E>(error: E): Err<E> => ({ ok: false, error });

Expand Down

0 comments on commit 043d624

Please sign in to comment.