@murky-web/typebuddy 2.0.0 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +184 -72
- package/dist/collections.js +1 -2
- package/dist/empty.js +34 -13
- package/dist/guards.js +33 -9
- package/dist/ids.js +6 -6
- package/dist/oxlint.js +7 -7
- package/dist/parse.js +39 -15
- package/dist/result.js +9 -8
- package/dist/rules/async_rule.js +45 -28
- package/dist/rules/deliberate_throw.js +5 -6
- package/dist/rules/maybe_promise_rule.js +109 -47
- package/dist/rules/maybe_rule.js +7 -5
- package/dist/rules/nullable_rule.js +7 -5
- package/dist/rules/optional_rule.js +8 -4
- package/dist/rules/own_subtree.js +1 -2
- package/dist/rules/test_file.js +63 -0
- package/dist/rules/typebuddy_import.js +242 -35
- package/dist/rules/use_server.js +63 -0
- package/globals.d.ts +49 -44
- package/jsr.json +3 -2
- package/oxlint/index.ts +7 -5
- package/package.json +92 -91
- package/rules/async_rule.ts +87 -67
- package/rules/deliberate_throw.ts +11 -11
- package/rules/maybe_promise_rule.ts +229 -98
- package/rules/maybe_rule.ts +74 -47
- package/rules/nullable_rule.ts +78 -50
- package/rules/optional_rule.ts +80 -51
- package/rules/own_subtree.ts +33 -33
- package/rules/test_file.ts +114 -0
- package/rules/typebuddy_import.ts +479 -87
- package/rules/use_server.ts +117 -0
- package/src/empty.ts +57 -19
- package/src/guards.ts +96 -30
- package/src/ids.ts +10 -4
- package/src/index.ts +1 -18
- package/src/parse.ts +104 -25
- package/src/public_api.ts +22 -0
- package/src/result.ts +11 -9
- package/src/types/globals.ts +11 -11
- package/src/types/index.ts +21 -21
- package/src/types/json.ts +48 -11
- package/src/types/maybe_promise.ts +17 -12
- package/src/types/nullable.ts +2 -2
- package/src/types/optional.ts +6 -6
package/src/parse.ts
CHANGED
|
@@ -6,15 +6,51 @@
|
|
|
6
6
|
|
|
7
7
|
import {
|
|
8
8
|
isArray,
|
|
9
|
-
isEmptyObject,
|
|
10
9
|
isNull,
|
|
11
10
|
isNumber,
|
|
11
|
+
isObject,
|
|
12
12
|
isString,
|
|
13
13
|
isUndefined,
|
|
14
14
|
} from "./guards.js";
|
|
15
15
|
import type { Optional } from "./types/optional.js";
|
|
16
16
|
|
|
17
17
|
const EMPTY_LENGTH = 0;
|
|
18
|
+
const ZERO = 0;
|
|
19
|
+
|
|
20
|
+
// Whole-string shapes. Anchored on both ends: a string is a number or it is
|
|
21
|
+
// not, never "a number followed by something".
|
|
22
|
+
const INTEGER_TEXT = /^[+-]?\d+(?:\.0+)?$/;
|
|
23
|
+
const DECIMAL_TEXT = /^[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?$/;
|
|
24
|
+
// A comma that is the only separator: `1,5`, `0,25`, `,5`.
|
|
25
|
+
const COMMA_DECIMAL_TEXT = /^[+-]?\d*,\d+$/;
|
|
26
|
+
// `1,000`, `12,345`: exactly three digits after the comma behind a short
|
|
27
|
+
// integer part read as a thousands group in English and as a decimal in German.
|
|
28
|
+
// Guessing wrong silently is worse than refusing, so these are refused. An
|
|
29
|
+
// integer part of a lone `0` is no thousands group (nobody writes `0,500` for
|
|
30
|
+
// five hundred), so `0,500` and `0,125` stay decimals.
|
|
31
|
+
const GROUPED_TEXT = /^[+-]?[1-9]\d{0,2},\d{3}$/;
|
|
32
|
+
|
|
33
|
+
// Turns a trimmed string into dot-decimal notation, or `undefined` when the
|
|
34
|
+
// comma in it cannot be told from a thousands separator.
|
|
35
|
+
function toDotDecimal(text: string): string | undefined {
|
|
36
|
+
if (!COMMA_DECIMAL_TEXT.test(text)) {
|
|
37
|
+
return text;
|
|
38
|
+
}
|
|
39
|
+
if (GROUPED_TEXT.test(text)) {
|
|
40
|
+
return undefined;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return text.replace(",", ".");
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// `-0` is the integer zero; callers comparing with `Object.is` never see it.
|
|
47
|
+
function withoutNegativeZero(value: number): number {
|
|
48
|
+
if (value === ZERO) {
|
|
49
|
+
return ZERO;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return value;
|
|
53
|
+
}
|
|
18
54
|
|
|
19
55
|
function cloneDefaultArray(
|
|
20
56
|
defaultValue?: readonly unknown[],
|
|
@@ -27,8 +63,13 @@ function cloneDefaultArray(
|
|
|
27
63
|
}
|
|
28
64
|
|
|
29
65
|
/**
|
|
30
|
-
* Parses the input value as an integer.
|
|
31
|
-
*
|
|
66
|
+
* Parses the input value as an integer. A number is truncated toward zero
|
|
67
|
+
* (`-1.5` is `-1`). A string has to be an integer written out in full: digits
|
|
68
|
+
* with an optional sign, surrounding whitespace and a zero-only fraction
|
|
69
|
+
* (`"42.0"`). Empty or blank strings, `"12abc"`, `"0x10"`, `"1e3"`, `"1,000"`
|
|
70
|
+
* and digits beyond the safe integer range (`"9007199254740993"`, also as a
|
|
71
|
+
* number: `1e21`) are not integers. Returns the default (or `undefined`) when
|
|
72
|
+
* the value cannot be parsed.
|
|
32
73
|
*
|
|
33
74
|
* @param {unknown} value - The value to check.
|
|
34
75
|
* @param {Optional<number>} defaultValue - Value gets returned if integer could
|
|
@@ -39,20 +80,38 @@ function parseInteger(value: unknown): Optional<number>;
|
|
|
39
80
|
function parseInteger(value: unknown, defaultValue: number): number;
|
|
40
81
|
function parseInteger(value: unknown, defaultValue?: number): Optional<number> {
|
|
41
82
|
if (isNumber(value)) {
|
|
42
|
-
|
|
83
|
+
// The same limit as for strings: past 2^53 a double stands for a range
|
|
84
|
+
// of integers, not for one, so there is no integer to answer with.
|
|
85
|
+
const truncated = Math.trunc(value);
|
|
86
|
+
if (Number.isSafeInteger(truncated)) {
|
|
87
|
+
return withoutNegativeZero(truncated);
|
|
88
|
+
}
|
|
89
|
+
return defaultValue;
|
|
43
90
|
}
|
|
44
91
|
|
|
45
92
|
if (isString(value)) {
|
|
46
|
-
const
|
|
47
|
-
if (
|
|
48
|
-
|
|
93
|
+
const trimmed = value.trim();
|
|
94
|
+
if (INTEGER_TEXT.test(trimmed)) {
|
|
95
|
+
const parsed = Number(trimmed);
|
|
96
|
+
// Past 2^53 the digits no longer fit a double: `"9007199254740993"`
|
|
97
|
+
// would come back as `...992`, a different number than the one
|
|
98
|
+
// written. Refusing it is better than answering with that.
|
|
99
|
+
if (Number.isSafeInteger(parsed)) {
|
|
100
|
+
return withoutNegativeZero(parsed);
|
|
101
|
+
}
|
|
49
102
|
}
|
|
50
103
|
}
|
|
51
104
|
return defaultValue;
|
|
52
105
|
}
|
|
53
106
|
|
|
54
107
|
/**
|
|
55
|
-
* Parses the input value as a float.
|
|
108
|
+
* Parses the input value as a float. A string has to be a decimal number in
|
|
109
|
+
* full: `"1.5"`, `".5"`, `"-2"`, `"1.5e2"`. A comma is accepted as the one and
|
|
110
|
+
* only decimal separator (`"1,5"`); mixed forms (`"1,234.56"`, `"1.234,56"`),
|
|
111
|
+
* several commas and grouped forms (`"1,000"`) are refused, because the comma
|
|
112
|
+
* could be a thousands separator. Empty or blank strings, `"12abc"`, `"0x10"`
|
|
113
|
+
* and `"Infinity"` are refused too. Returns the default (or `undefined`) when
|
|
114
|
+
* the value cannot be parsed.
|
|
56
115
|
*
|
|
57
116
|
* @param {unknown} value - The value to check.
|
|
58
117
|
* @param {Optional<number>} defaultValue - Value gets returned if float could
|
|
@@ -63,21 +122,27 @@ function parseFloat(value: unknown): Optional<number>;
|
|
|
63
122
|
function parseFloat(value: unknown, defaultValue: number): number;
|
|
64
123
|
function parseFloat(value: unknown, defaultValue?: number): Optional<number> {
|
|
65
124
|
if (isNumber(value)) {
|
|
66
|
-
return value;
|
|
125
|
+
return withoutNegativeZero(value);
|
|
67
126
|
}
|
|
68
127
|
if (isString(value)) {
|
|
69
|
-
const
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
128
|
+
const dotted = toDotDecimal(value.trim());
|
|
129
|
+
if (dotted !== undefined && DECIMAL_TEXT.test(dotted)) {
|
|
130
|
+
const parsed = Number(dotted);
|
|
131
|
+
if (Number.isFinite(parsed)) {
|
|
132
|
+
return withoutNegativeZero(parsed);
|
|
133
|
+
}
|
|
73
134
|
}
|
|
74
135
|
}
|
|
75
136
|
return defaultValue;
|
|
76
137
|
}
|
|
77
138
|
|
|
78
139
|
/**
|
|
79
|
-
* Parses the input value as a number.
|
|
80
|
-
*
|
|
140
|
+
* Parses the input value as a number. The most permissive of the three: a
|
|
141
|
+
* string may be anything `Number()` accepts for the whole string, so also
|
|
142
|
+
* `"1e3"`, `"0xff"` and `"0b10"`. The comma rule is the one of `parseFloat`
|
|
143
|
+
* (`"1,5"` is `1.5`, `"1,000"` and `"1,234.56"` are refused). Empty or blank
|
|
144
|
+
* strings, `"12abc"` and `"Infinity"` are refused. Returns the default (or
|
|
145
|
+
* `undefined`) when the value cannot be parsed.
|
|
81
146
|
*
|
|
82
147
|
* @param {unknown} value - The value to check.
|
|
83
148
|
* @param {Optional<number>} defaultValue - Value gets returned if number could
|
|
@@ -88,13 +153,15 @@ function parseNumber(value: unknown): Optional<number>;
|
|
|
88
153
|
function parseNumber(value: unknown, defaultValue: number): number;
|
|
89
154
|
function parseNumber(value: unknown, defaultValue?: number): Optional<number> {
|
|
90
155
|
if (isNumber(value)) {
|
|
91
|
-
return value;
|
|
156
|
+
return withoutNegativeZero(value);
|
|
92
157
|
}
|
|
93
158
|
if (isString(value)) {
|
|
94
|
-
const
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
159
|
+
const dotted = toDotDecimal(value.trim());
|
|
160
|
+
if (dotted !== undefined && dotted !== "") {
|
|
161
|
+
const parsed = Number(dotted);
|
|
162
|
+
if (Number.isFinite(parsed)) {
|
|
163
|
+
return withoutNegativeZero(parsed);
|
|
164
|
+
}
|
|
98
165
|
}
|
|
99
166
|
}
|
|
100
167
|
return defaultValue;
|
|
@@ -123,8 +190,12 @@ function parseString(value: unknown, defaultValue = ""): string {
|
|
|
123
190
|
}
|
|
124
191
|
|
|
125
192
|
/**
|
|
126
|
-
* Parses the input value as an array.
|
|
127
|
-
*
|
|
193
|
+
* Parses the input value as an array. An array is copied, a string is split on
|
|
194
|
+
* commas, pipes, semicolons and whitespace, a finite number or a plain object
|
|
195
|
+
* becomes a one-element array. Anything else (`null`, `NaN`, `Infinity`, a
|
|
196
|
+
* class instance, a symbol) gives the default, or `undefined` without one: a
|
|
197
|
+
* single-argument call with a number is therefore typed `number[] | undefined`,
|
|
198
|
+
* since `NaN` has no array.
|
|
128
199
|
*
|
|
129
200
|
* @param {unknown} value - The value to check.
|
|
130
201
|
* @param {Optional<T[]>} defaultValue - Value gets returned if array could not
|
|
@@ -134,7 +205,7 @@ function parseString(value: unknown, defaultValue = ""): string {
|
|
|
134
205
|
|
|
135
206
|
function parseArray<T>(value: readonly T[]): T[];
|
|
136
207
|
function parseArray(value: string): string[];
|
|
137
|
-
function parseArray(value: number): number[];
|
|
208
|
+
function parseArray(value: number): number[] | undefined;
|
|
138
209
|
function parseArray<T extends Record<string, unknown>>(value: T): T[];
|
|
139
210
|
function parseArray<T>(value: unknown, defaultValue: readonly T[]): T[];
|
|
140
211
|
function parseArray(value: unknown): unknown[] | undefined;
|
|
@@ -159,7 +230,7 @@ function parseArray(
|
|
|
159
230
|
if (isNumber(value)) {
|
|
160
231
|
return [value];
|
|
161
232
|
}
|
|
162
|
-
if (
|
|
233
|
+
if (isObject(value)) {
|
|
163
234
|
return [value];
|
|
164
235
|
}
|
|
165
236
|
|
|
@@ -170,7 +241,15 @@ function parseArray(
|
|
|
170
241
|
}
|
|
171
242
|
|
|
172
243
|
/**
|
|
173
|
-
* Parses the
|
|
244
|
+
* Parses the first label of the hostname of a URL, without a leading `www.` or
|
|
245
|
+
* `www2.`: `"https://www.example.com/x"` gives `"example"`.
|
|
246
|
+
*
|
|
247
|
+
* This is a display helper, not a registrable-domain check. It returns the
|
|
248
|
+
* LEFTMOST label, so `"example.attacker.net"` gives `"example"` as well, and
|
|
249
|
+
* `"subdomain.example.dev"` gives `"subdomain"`. It knows nothing about the
|
|
250
|
+
* public-suffix list. Never use it to decide whether a host belongs to a site,
|
|
251
|
+
* for an allowlist, a redirect check or a CORS origin: compare the whole
|
|
252
|
+
* hostname (`new URL(url).hostname`) against the exact hosts you trust.
|
|
174
253
|
*
|
|
175
254
|
* @param {string} url - The URL to parse.
|
|
176
255
|
* @param {Optional<T>} defaultValue - The fallback when parsing fails.
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
// Everything the root entry exports, without the global type augmentation.
|
|
2
|
+
// `index.ts` adds the augmentation on top of this file for npm; JSR publishes
|
|
3
|
+
// this file as its root export, because JSR rejects `declare global`.
|
|
4
|
+
|
|
5
|
+
export * from "./type_helper.js";
|
|
6
|
+
|
|
7
|
+
export type {
|
|
8
|
+
AsyncResult,
|
|
9
|
+
Failed,
|
|
10
|
+
JsonifiedObject,
|
|
11
|
+
JsonifiedValue,
|
|
12
|
+
Maybe,
|
|
13
|
+
MaybePromise,
|
|
14
|
+
Nullable,
|
|
15
|
+
Optional,
|
|
16
|
+
ResolveMaybe,
|
|
17
|
+
ResolveNullable,
|
|
18
|
+
ResolveOptional,
|
|
19
|
+
Result,
|
|
20
|
+
Stringified,
|
|
21
|
+
Success,
|
|
22
|
+
} from "./types/index.js";
|
package/src/result.ts
CHANGED
|
@@ -13,20 +13,22 @@ import type { Failed, Result, Success } from "./types/maybe_promise.js";
|
|
|
13
13
|
* ```typescript
|
|
14
14
|
* const result = await someAsyncFunction();
|
|
15
15
|
* if (isSuccess(result)) {
|
|
16
|
-
*
|
|
16
|
+
* return result.value; // TypeScript knows `value` is of type `T`
|
|
17
17
|
* }
|
|
18
18
|
* ```
|
|
19
19
|
*
|
|
20
|
-
* @template
|
|
21
|
-
*
|
|
22
|
-
*
|
|
20
|
+
* @template R The result type passed in. The guard narrows it by its `isError`
|
|
21
|
+
* flag, so `value` keeps the success type without an extra `| null`. A
|
|
22
|
+
* `Result<T | null>`, a generic `Result<T>` and a `Result<any>` all work.
|
|
23
|
+
* @param {R} result The result to check, which can be either a `Success<T>` or
|
|
24
|
+
* `Failed`.
|
|
23
25
|
* @returns {boolean} `true` if the result is a `Success<T>`, `false` if it is a
|
|
24
|
-
* `Failed`. This function acts as a type guard, narrowing
|
|
25
|
-
* `
|
|
26
|
+
* `Failed`. This function acts as a type guard, narrowing `result` to its
|
|
27
|
+
* `Success` member when the condition is met.
|
|
26
28
|
*/
|
|
27
|
-
function isSuccess<
|
|
28
|
-
result:
|
|
29
|
-
): result is
|
|
29
|
+
function isSuccess<R extends Readonly<Result<unknown>>>(
|
|
30
|
+
result: R,
|
|
31
|
+
): result is Extract<R, { isError: false }> {
|
|
30
32
|
return !result.isError;
|
|
31
33
|
}
|
|
32
34
|
|
package/src/types/globals.ts
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
export type {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
2
|
+
Failed,
|
|
3
|
+
JsonifiedValue,
|
|
4
|
+
Maybe,
|
|
5
|
+
MaybePromise,
|
|
6
|
+
Nullable,
|
|
7
|
+
Optional,
|
|
8
|
+
ResolveMaybe,
|
|
9
|
+
ResolveNullable,
|
|
10
|
+
ResolveOptional,
|
|
11
|
+
Stringified,
|
|
12
|
+
Success,
|
|
13
13
|
} from "./index.js";
|
package/src/types/index.ts
CHANGED
|
@@ -1,28 +1,28 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
AsyncResult,
|
|
3
|
-
Failed,
|
|
4
|
-
MaybePromise,
|
|
5
|
-
Result,
|
|
6
|
-
Success,
|
|
7
|
-
} from "./maybe_promise.js";
|
|
8
1
|
import type { JsonifiedObject, JsonifiedValue, Stringified } from "./json.js";
|
|
9
2
|
import type { Maybe, ResolveMaybe } from "./maybe.js";
|
|
3
|
+
import type {
|
|
4
|
+
AsyncResult,
|
|
5
|
+
Failed,
|
|
6
|
+
MaybePromise,
|
|
7
|
+
Result,
|
|
8
|
+
Success,
|
|
9
|
+
} from "./maybe_promise.js";
|
|
10
10
|
import type { Nullable, ResolveNullable } from "./nullable.js";
|
|
11
11
|
import type { Optional, ResolveOptional } from "./optional.js";
|
|
12
12
|
|
|
13
13
|
export type {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
14
|
+
AsyncResult,
|
|
15
|
+
Failed,
|
|
16
|
+
JsonifiedObject,
|
|
17
|
+
JsonifiedValue,
|
|
18
|
+
Maybe,
|
|
19
|
+
MaybePromise,
|
|
20
|
+
Nullable,
|
|
21
|
+
Optional,
|
|
22
|
+
ResolveMaybe,
|
|
23
|
+
ResolveNullable,
|
|
24
|
+
ResolveOptional,
|
|
25
|
+
Result,
|
|
26
|
+
Stringified,
|
|
27
|
+
Success,
|
|
28
28
|
};
|
package/src/types/json.ts
CHANGED
|
@@ -1,19 +1,56 @@
|
|
|
1
1
|
type UnknownFunction = (...args: readonly unknown[]) => unknown;
|
|
2
2
|
|
|
3
|
+
/** What `JSON.stringify` serialises to `{}`: it has no enumerable own data. */
|
|
4
|
+
type EmptyJsonObject = Record<string, never>;
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Array and tuple elements JSON cannot hold become `null` instead of vanishing
|
|
8
|
+
* (`JSON.stringify([undefined])` is `"[null]"`), so the element type keeps a
|
|
9
|
+
* `null` where an object property would be dropped.
|
|
10
|
+
*/
|
|
11
|
+
type JsonifiedElement<T> = T extends undefined | symbol | UnknownFunction
|
|
12
|
+
? null
|
|
13
|
+
: JsonifiedValue<T>;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Arrays and tuples are mapped element by element with a plain homomorphic
|
|
17
|
+
* mapped type, which keeps them arrays and tuples. `JsonifiedObject` cannot do
|
|
18
|
+
* this: its key remapping turns an array into an object type without `map`. The
|
|
19
|
+
* result is mutable, because `JSON.parse` builds fresh arrays.
|
|
20
|
+
*/
|
|
21
|
+
type JsonifiedArray<T extends readonly unknown[]> = {
|
|
22
|
+
-readonly [Index in keyof T]: JsonifiedElement<T[Index]>;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* The type `JSON.parse` gives back for a value that went through
|
|
27
|
+
* `JSON.stringify`.
|
|
28
|
+
*
|
|
29
|
+
* `number` stays `number`: the type system has no way to say "not NaN or
|
|
30
|
+
* Infinity", and `JSON.stringify` turns those two into `null` at runtime.
|
|
31
|
+
*/
|
|
3
32
|
type JsonifiedValue<T> = T extends string | number | null | boolean
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
33
|
+
? T
|
|
34
|
+
: T extends { toJSON: () => infer R }
|
|
35
|
+
? R
|
|
36
|
+
: T extends undefined | symbol | UnknownFunction
|
|
37
|
+
? never
|
|
38
|
+
: T extends readonly unknown[]
|
|
39
|
+
? JsonifiedArray<T>
|
|
40
|
+
: T extends
|
|
41
|
+
| ReadonlyMap<unknown, unknown>
|
|
42
|
+
| ReadonlySet<unknown>
|
|
43
|
+
| WeakMap<WeakKey, unknown>
|
|
44
|
+
| WeakSet<WeakKey>
|
|
45
|
+
? EmptyJsonObject
|
|
46
|
+
: T extends object
|
|
47
|
+
? JsonifiedObject<T>
|
|
48
|
+
: never;
|
|
12
49
|
|
|
13
50
|
type JsonifiedObject<T> = {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
51
|
+
[
|
|
52
|
+
Key in keyof T as [JsonifiedValue<T[Key]>] extends [never] ? never : Key
|
|
53
|
+
]: JsonifiedValue<T[Key]>;
|
|
17
54
|
};
|
|
18
55
|
|
|
19
56
|
// Stringified type for JSON.stringify to return as a string with a source type
|
|
@@ -1,21 +1,26 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* A successful outcome. `isError` is always `false`, whatever `value` is: a
|
|
3
|
+
* success may carry `null` (`ok(null)`), and the flag must not flip with the
|
|
4
|
+
* payload.
|
|
5
|
+
*/
|
|
2
6
|
type Success<T> = {
|
|
3
|
-
|
|
4
|
-
|
|
7
|
+
isError: false;
|
|
8
|
+
value: T;
|
|
5
9
|
};
|
|
6
10
|
|
|
11
|
+
/** A failed outcome. `isError` is always `true` and `value` is `null`. */
|
|
7
12
|
type Failed<T extends null = null> = {
|
|
8
|
-
|
|
9
|
-
|
|
13
|
+
isError: true;
|
|
14
|
+
value: T;
|
|
10
15
|
};
|
|
11
16
|
|
|
12
17
|
/**
|
|
13
18
|
* A settled outcome: the value, or the failure.
|
|
14
19
|
*
|
|
15
20
|
* This is what a `MaybePromise` resolves to, and it needed a name of its own.
|
|
16
|
-
* Without one, anything that holds an already-awaited outcome has to restate
|
|
17
|
-
* union or reach for `Awaited<...>` to undo a wrapper that is no longer
|
|
18
|
-
* which describes the route taken rather than the thing being held.
|
|
21
|
+
* Without one, anything that holds an already-awaited outcome has to restate
|
|
22
|
+
* the union or reach for `Awaited<...>` to undo a wrapper that is no longer
|
|
23
|
+
* there — which describes the route taken rather than the thing being held.
|
|
19
24
|
*/
|
|
20
25
|
type Result<T> = Success<T> | Failed;
|
|
21
26
|
|
|
@@ -24,12 +29,12 @@ type MaybePromise<T> = Promise<Result<T>>;
|
|
|
24
29
|
/**
|
|
25
30
|
* `MaybePromise` under a name that says what it is.
|
|
26
31
|
*
|
|
27
|
-
* Elsewhere in the ecosystem `MaybePromise<T>` almost always means
|
|
28
|
-
*
|
|
32
|
+
* Elsewhere in the ecosystem `MaybePromise<T>` almost always means `T |
|
|
33
|
+
* Promise<T>` — a value that may or may not be a promise. Here it means a
|
|
29
34
|
* promise of a `Result`, which is a different thing entirely, so a reader who
|
|
30
35
|
* knows the other convention reads this one wrong. Prefer `AsyncResult` in new
|
|
31
|
-
* code. `MaybePromise` keeps working and is what the `prefer-maybe-promise`
|
|
32
|
-
* still suggests.
|
|
36
|
+
* code. `MaybePromise` keeps working and is what the `prefer-maybe-promise`
|
|
37
|
+
* rule still suggests.
|
|
33
38
|
*/
|
|
34
39
|
type AsyncResult<T> = MaybePromise<T>;
|
|
35
40
|
|
package/src/types/nullable.ts
CHANGED
|
@@ -2,7 +2,7 @@ import type { Maybe } from "./maybe.js";
|
|
|
2
2
|
import type { Optional } from "./optional.js";
|
|
3
3
|
type Nullable<N> = Optional<N> | Maybe<N>;
|
|
4
4
|
type ResolveNullable<T, R extends Nullable<T>> = R extends null | undefined
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
? Nullable<T>
|
|
6
|
+
: T;
|
|
7
7
|
|
|
8
8
|
export type { Nullable, ResolveNullable };
|
package/src/types/optional.ts
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
type Optional<T> = T | undefined;
|
|
2
2
|
|
|
3
3
|
type ResolveOptionalArray<T, R extends Optional<T>> = R extends undefined
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
? Optional<T>
|
|
5
|
+
: T;
|
|
6
6
|
|
|
7
7
|
type ResolveBasicOptional<T, R extends Optional<T>> = R extends undefined
|
|
8
|
-
|
|
9
|
-
|
|
8
|
+
? Optional<T>
|
|
9
|
+
: T;
|
|
10
10
|
|
|
11
11
|
type ResolveOptional<T, R extends Optional<T>> = T extends unknown[]
|
|
12
|
-
|
|
13
|
-
|
|
12
|
+
? ResolveOptionalArray<T, R>
|
|
13
|
+
: ResolveBasicOptional<T, R>;
|
|
14
14
|
|
|
15
15
|
export type { Optional, ResolveOptional };
|