Skip to content

Commit 26c623c

Browse files
committed
fixes #16: lets ts narrow err/ok cases in else clauses
1 parent 4be9014 commit 26c623c

File tree

6 files changed

+320
-125
lines changed

6 files changed

+320
-125
lines changed

package-lock.json

Lines changed: 68 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 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.3.0",
4+
"version": "3.4.0-beta.1",
55
"description": "A Result type inspired by Rust and Kotlin that leverages TypeScript's powerful type system to simplify error handling and make your code more readable and maintainable.",
66
"keywords": [
77
"result",
@@ -62,6 +62,7 @@
6262
"@types/node": "^24.0.8",
6363
"@vitest/coverage-v8": "^3.2.4",
6464
"husky": "^9.1.7",
65+
"terser": "^5.43.1",
6566
"tsup": "^8.5.0",
6667
"typescript": "^5.8.3",
6768
"vitest": "^3.2.4",

src/index.ts

Lines changed: 72 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,73 @@
1-
/* v8 ignore next */
21
export { assertUnreachable } from "./helpers.js";
3-
/* v8 ignore next */
4-
export { AsyncResult, Result } from "./result.js";
2+
3+
import {
4+
type AsyncResult,
5+
type IfGeneratorAsync,
6+
type InferGeneratorError,
7+
type InferGeneratorReturn,
8+
Result as ResultBase,
9+
} from "./result.js";
10+
11+
export { AsyncResult } from "./result.js";
12+
13+
export namespace Result {
14+
export type Error<E> = ResultBase<never, E>;
15+
export type Ok<V> = ResultBase<V, never>;
16+
export type InferError<T> = T extends AsyncResult<any, infer Error>
17+
? Error
18+
: T extends Result<any, infer Error>
19+
? Error
20+
: never;
21+
export type InferValue<T> = T extends AsyncResult<infer Value, any>
22+
? Value
23+
: T extends Result<infer Value, any>
24+
? Value
25+
: T;
26+
export type InferResultFromGenerator<T> = T extends Generator | AsyncGenerator
27+
? IfGeneratorAsync<
28+
T,
29+
AsyncResult<InferGeneratorReturn<T>, InferGeneratorError<T>>,
30+
Result<InferGeneratorReturn<T>, InferGeneratorError<T>>
31+
>
32+
: never;
33+
}
34+
35+
export type Result<Value, Err> =
36+
| ([Value] extends [never] ? never : Result.Ok<Value>)
37+
| ([Err] extends [never] ? never : Result.Error<Err>);
38+
39+
export const Result: {
40+
ok: typeof ResultBase.ok;
41+
error: typeof ResultBase.error;
42+
assertOk: typeof ResultBase.assertOk;
43+
assertError: typeof ResultBase.assertError;
44+
isResult: typeof ResultBase.isResult;
45+
isAsyncResult: typeof ResultBase.isAsyncResult;
46+
all: typeof ResultBase.all;
47+
allCatching: typeof ResultBase.allCatching;
48+
wrap: typeof ResultBase.wrap;
49+
try: typeof ResultBase.try;
50+
fromAsync: typeof ResultBase.fromAsync;
51+
fromAsyncCatching: typeof ResultBase.fromAsyncCatching;
52+
gen: typeof ResultBase.gen;
53+
genCatching: typeof ResultBase.genCatching;
54+
[Symbol.hasInstance]: (instance: unknown) => boolean;
55+
} = {
56+
ok: ResultBase.ok,
57+
error: ResultBase.error,
58+
isResult: ResultBase.isResult,
59+
isAsyncResult: ResultBase.isAsyncResult,
60+
all: ResultBase.all,
61+
allCatching: ResultBase.allCatching,
62+
wrap: ResultBase.wrap,
63+
try: ResultBase.try,
64+
fromAsync: ResultBase.fromAsync,
65+
fromAsyncCatching: ResultBase.fromAsyncCatching,
66+
gen: ResultBase.gen,
67+
genCatching: ResultBase.genCatching,
68+
assertOk: ResultBase.assertOk,
69+
assertError: ResultBase.assertError,
70+
[Symbol.hasInstance](instance: unknown) {
71+
return instance instanceof ResultBase;
72+
},
73+
};

0 commit comments

Comments
 (0)