Skip to content

Commit 8a3f661

Browse files
committed
fixes #19: provides fallback T for ExtractValues<T>
1 parent 7a36cd9 commit 8a3f661

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"private": false,
33
"name": "typescript-result",
4-
"version": "3.5.0",
4+
"version": "3.5.1",
55
"description": "Supercharge your TypeScript error handling with a powerful Result type that transforms chaotic try-catch blocks into elegant, type-safe code.",
66
"keywords": [
77
"result",

src/result.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2862,6 +2862,23 @@ describe("Result", () => {
28622862
);
28632863
expectTypeOf(resultE).toEqualTypeOf<AsyncResult<number, never>>();
28642864
});
2865+
2866+
it("Correctly infers the ok-value when there's some overlap with an Result instance", () => {
2867+
// See: https://github.com/everweij/typescript-result/issues/19
2868+
2869+
// { value: string } has some overlap with the Result type, so it seems like
2870+
// TS is very strict with `ReturningValue<T>` and returns never. The workaround seem
2871+
// to be to give `ExtractValue<T>` a fallback of `T`.
2872+
const resultA = Result.ok(12).map(() => ({ value: "bar" }));
2873+
2874+
expectTypeOf(resultA).toEqualTypeOf<Result.Ok<{ value: string }>>();
2875+
2876+
const resultB = Result.try(() => ({ data: { value: "string" } })).map(
2877+
({ data }) => data,
2878+
);
2879+
2880+
expectTypeOf(resultB).toEqualTypeOf<Result<{ value: string }, Error>>();
2881+
});
28652882
});
28662883

28672884
describe("AsyncResult", () => {

src/result.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ type ReturningError<T> =
4242
| AsyncResult<any, T>
4343
| Promise<ReturningError<T>>;
4444

45-
type ExtractValue<T> = T extends ReturningValue<infer Value> ? Value : never;
45+
type ExtractValue<T> = T extends ReturningValue<infer Value> ? Value : T;
4646
type ExtractError<T> = T extends ReturningError<infer Error> ? Error : never;
4747

4848
type ExtractValues<T extends any[]> = {

0 commit comments

Comments
 (0)