Skip to content

Commit ee3ace1

Browse files
committed
fixes type issue where Result.Error<E> and Result.Ok<V> is not interchangeable with Result<never, E> and Result<V, never> respectively
1 parent 26e86ac commit ee3ace1

File tree

4 files changed

+8
-32
lines changed

4 files changed

+8
-32
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.2",
4+
"version": "3.5.3-beta.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/index.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@ export { NonExhaustiveError } from "./matcher.js";
1313
export { AsyncResult } from "./result.js";
1414

1515
export namespace Result {
16-
export type Error<E> = ResultBase<never, E>;
17-
export type Ok<V> = ResultBase<V, never>;
16+
type ResultOk<V> = ResultBase<V, never>;
17+
type ResultError<E> = ResultBase<never, E>;
18+
19+
export type Ok<Value> = [Value] extends [never] ? never : ResultOk<Value>;
20+
export type Error<E> = [E] extends [never] ? never : ResultError<E>;
1821
export type InferError<T> = T extends AsyncResult<any, infer Error>
1922
? Error
2023
: T extends Result<any, infer Error>
@@ -34,9 +37,6 @@ export namespace Result {
3437
: never;
3538
}
3639

37-
type Ok<Value> = [Value] extends [never] ? never : Result.Ok<Value>;
38-
type Error<Err> = [Err] extends [never] ? never : Result.Error<Err>;
39-
40-
export type Result<Value, Err> = Ok<Value> | Error<Err>;
40+
export type Result<Value, Err> = Result.Ok<Value> | Result.Error<Err>;
4141

4242
export const Result: typeof ResultFactory = ResultFactory;

src/result.test.ts

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3785,27 +3785,3 @@ describe("AsyncResult", () => {
37853785
});
37863786
});
37873787
});
3788-
3789-
// function bla() {
3790-
// if (Math.random() > 0.5) {
3791-
// return Result.ok(42);
3792-
// }
3793-
3794-
// if (Math.random() > 0.5) {
3795-
// return Result.error(new ErrorB());
3796-
// }
3797-
3798-
// return Result.error(new ErrorA());
3799-
// }
3800-
3801-
// const x = bla().map((val) => val * 2);
3802-
3803-
// if (x.isOk()) {
3804-
// x; //=>
3805-
// expectTypeOf(x.value).toBeNumber();
3806-
// expectTypeOf(x.error).toBeUndefined();
3807-
// } else {
3808-
// x; //=>
3809-
// expectTypeOf(x.value).toBeUndefined();
3810-
// expectTypeOf(x.error).toEqualTypeOf<ErrorA | ErrorB>();
3811-
// }

src/result.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1640,7 +1640,7 @@ export class ResultFactory {
16401640
*/
16411641
static error<const Err extends string>(error: Err): OuterResult.Error<Err>;
16421642
static error<Err>(error: Err): OuterResult.Error<Err>;
1643-
static error<Err>(error: Err): OuterResult.Error<Err> {
1643+
static error<Err>(error: Err) {
16441644
return new Result(undefined as never, error);
16451645
}
16461646

0 commit comments

Comments
 (0)