Skip to content

Commit

Permalink
Merge pull request #584 from macksal/macksal/ok-void-argument
Browse files Browse the repository at this point in the history
Allow ok/err/okAsync/errAsync to accept zero arguments when returning void
  • Loading branch information
supermacro authored Nov 9, 2024
2 parents 0252e44 + 7767eab commit fd35945
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/thirty-avocados-explain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"neverthrow": minor
---

Allow ok/err/okAsync/errAsync to accept zero arguments when returning void
14 changes: 10 additions & 4 deletions src/result-async.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,11 +228,17 @@ export class ResultAsync<T, E> implements PromiseLike<Result<T, E>> {
}
}

export const okAsync = <T, E = never>(value: T): ResultAsync<T, E> =>
new ResultAsync(Promise.resolve(new Ok<T, E>(value)))
export function okAsync<T, E = never>(value: T): ResultAsync<T, E>
export function okAsync<T extends void = void, E = never>(value: void): ResultAsync<void, E>
export function okAsync<T, E = never>(value: T): ResultAsync<T, E> {
return new ResultAsync(Promise.resolve(new Ok<T, E>(value)))
}

export const errAsync = <T = never, E = unknown>(err: E): ResultAsync<T, E> =>
new ResultAsync(Promise.resolve(new Err<T, E>(err)))
export function errAsync<T = never, E = unknown>(err: E): ResultAsync<T, E>
export function errAsync<T = never, E extends void = void>(err: void): ResultAsync<T, void>
export function errAsync<T = never, E = unknown>(err: E): ResultAsync<T, E> {
return new ResultAsync(Promise.resolve(new Err<T, E>(err)))
}

export const fromPromise = ResultAsync.fromPromise
export const fromSafePromise = ResultAsync.fromSafePromise
Expand Down
7 changes: 6 additions & 1 deletion src/result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,15 @@ export namespace Result {

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

export const ok = <T, E = never>(value: T): Ok<T, E> => new Ok(value)
export function ok<T, E = never>(value: T): Ok<T, E>
export function ok<T extends void = void, E = never>(value: void): Ok<void, E>
export function ok<T, E = never>(value: T): Ok<T, E> {
return new Ok(value)
}

export function err<T = never, E extends string = string>(err: E): Err<T, E>
export function err<T = never, E = unknown>(err: E): Err<T, E>
export function err<T = never, E extends void = void>(err: void): Err<T, void>
export function err<T = never, E = unknown>(err: E): Err<T, E> {
return new Err(err)
}
Expand Down

0 comments on commit fd35945

Please sign in to comment.