diff --git a/.changeset/thirty-avocados-explain.md b/.changeset/thirty-avocados-explain.md new file mode 100644 index 00000000..9d1b36ae --- /dev/null +++ b/.changeset/thirty-avocados-explain.md @@ -0,0 +1,5 @@ +--- +"neverthrow": minor +--- + +Allow ok/err/okAsync/errAsync to accept zero arguments when returning void diff --git a/src/result-async.ts b/src/result-async.ts index 6afcb581..9c349c54 100644 --- a/src/result-async.ts +++ b/src/result-async.ts @@ -228,11 +228,17 @@ export class ResultAsync implements PromiseLike> { } } -export const okAsync = (value: T): ResultAsync => - new ResultAsync(Promise.resolve(new Ok(value))) +export function okAsync(value: T): ResultAsync +export function okAsync(value: void): ResultAsync +export function okAsync(value: T): ResultAsync { + return new ResultAsync(Promise.resolve(new Ok(value))) +} -export const errAsync = (err: E): ResultAsync => - new ResultAsync(Promise.resolve(new Err(err))) +export function errAsync(err: E): ResultAsync +export function errAsync(err: void): ResultAsync +export function errAsync(err: E): ResultAsync { + return new ResultAsync(Promise.resolve(new Err(err))) +} export const fromPromise = ResultAsync.fromPromise export const fromSafePromise = ResultAsync.fromSafePromise diff --git a/src/result.ts b/src/result.ts index 3faa8886..3f6f5a94 100644 --- a/src/result.ts +++ b/src/result.ts @@ -61,10 +61,15 @@ export namespace Result { export type Result = Ok | Err -export const ok = (value: T): Ok => new Ok(value) +export function ok(value: T): Ok +export function ok(value: void): Ok +export function ok(value: T): Ok { + return new Ok(value) +} export function err(err: E): Err export function err(err: E): Err +export function err(err: void): Err export function err(err: E): Err { return new Err(err) }