From 53d687cd5d837901cabaed755b1673e073e7d7d8 Mon Sep 17 00:00:00 2001 From: jcbkdev Date: Sat, 17 Oct 2026 02:54:19 +0200 Subject: [PATCH 1/2] fix: require a value in Result.ok() unless the type is Result --- src/result.test.ts | 11 +++++++++++ src/result.ts | 2 ++ 2 files changed, 13 insertions(+) diff --git a/src/result.test.ts b/src/result.test.ts index 43a80b5..8d3b899 100644 --- a/src/result.test.ts +++ b/src/result.test.ts @@ -21,6 +21,17 @@ describe('Result', () => { test('getError', () => { expect(result.getError()).toBeUndefined(); }); + + test('without a value is allowed for Result', () => { + const result: Result = Result.ok(); + expect(result.isOk()).toBe(true); + expect(result.getValue()).toBeUndefined(); + }); + + test('without a value is NOT assignable to Result', () => { + // @ts-expect-error: a missing value must not satisfy Result + const result: Result = Result.ok(); + }); }); describe('Result.error', () => { diff --git a/src/result.ts b/src/result.ts index 6b28c68..894caf6 100644 --- a/src/result.ts +++ b/src/result.ts @@ -21,6 +21,8 @@ class ResultImpl { this.error = error; } + static ok(): Result; + static ok(value: T): Result; static ok(value?: T): Result { return new ResultImpl(true, value) as Result; } From f23fd612204b6be83df8003a7c9a14cd49806692 Mon Sep 17 00:00:00 2001 From: Jakub Konieczny Date: Sat, 17 Oct 2026 03:02:53 +0200 Subject: [PATCH 2/2] docs: document Result.ok() with no value --- README.md | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 1254766..0dbd7fc 100644 --- a/README.md +++ b/README.md @@ -35,8 +35,7 @@ if (result.isError()) { if (result.isOk()) { console.log(result.getValue()); -} -else { +} else { console.error(result.getError()); } ``` @@ -47,7 +46,8 @@ else { | Method | Description | | --------------------- | -------------------------------------------------------- | -| `Result.ok(value?)` | Creates a successful result, optionally holding a value. | +| `Result.ok()` | Creates a successful `Result`, with no value. | +| `Result.ok(value)` | Creates a successful result, holding a value. | | `Result.error(error)` | Creates a failed result holding an `Error`. | ### Inspecting a result @@ -58,7 +58,6 @@ else { | `isError()` | `boolean` | `true` if the result is a failure. | | `getValue()` | `T \| undefined` | The value of a success. `undefined` for a failure. | | `getError()` | `Error \| undefined` | The error of a failure. `undefined` for a success. | -| | | | ## Notes @@ -89,6 +88,25 @@ function parseUrl(input: string): Result { } ``` +### Results without a value + +`Result.ok()` with no argument returns a `Result`. For any other type, pass the value explicitly, even if it is `undefined`: + +```ts +function deleteUser(id: string): Result { + db.remove(id); + return Result.ok(); +} + +function findUser(id: string): Result { + try { + return Result.ok(db.get(id)); // undefined if there is no such user + } catch (error) { + return Result.error(error instanceof Error ? error : new Error(String(error))); + } +} +``` + ## Contributing 1. Fork the repository.