@ls-stack/utils 3.40.0 → 3.41.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/dist/arrayUtils.d.cts +70 -57
- package/dist/arrayUtils.d.ts +70 -57
- package/dist/assertions.d.cts +128 -117
- package/dist/assertions.d.ts +128 -117
- package/dist/asyncQueue.cjs +117 -112
- package/dist/asyncQueue.d.cts +217 -215
- package/dist/asyncQueue.d.ts +217 -215
- package/dist/asyncQueue.js +117 -112
- package/dist/awaitDebounce.d.cts +26 -23
- package/dist/awaitDebounce.d.ts +26 -23
- package/dist/cache.cjs +2 -1
- package/dist/cache.d.cts +15 -9
- package/dist/cache.d.ts +15 -9
- package/dist/cache.js +2 -1
- package/dist/{chunk-GHAQOUA6.js → chunk-23KPGKDT.js} +22 -1
- package/dist/{chunk-ADM37GSC.js → chunk-7L4KCZJJ.js} +12 -10
- package/dist/{chunk-KW55OTUG.js → chunk-B3KFV2MH.js} +2 -2
- package/dist/chunk-DTE2QMWE.js +48 -0
- package/dist/concurrentCalls.d.cts +8 -4
- package/dist/concurrentCalls.d.ts +8 -4
- package/dist/concurrentCalls.js +1 -1
- package/dist/debounce.d.cts +12 -20
- package/dist/debounce.d.ts +12 -20
- package/dist/dedent.d.cts +31 -31
- package/dist/dedent.d.ts +31 -31
- package/dist/deepEqual.d.cts +10 -9
- package/dist/deepEqual.d.ts +10 -9
- package/dist/filterObjectOrArrayKeys.d.cts +44 -19
- package/dist/filterObjectOrArrayKeys.d.ts +44 -19
- package/dist/getAutoIncrementId.d.cts +25 -23
- package/dist/getAutoIncrementId.d.ts +25 -23
- package/dist/getCompositeKey.d.cts +2 -1
- package/dist/getCompositeKey.d.ts +2 -1
- package/dist/getValueStableKey.d.cts +4 -3
- package/dist/getValueStableKey.d.ts +4 -3
- package/dist/mutationUtils.d.cts +2 -2
- package/dist/mutationUtils.d.ts +2 -2
- package/dist/objUtils.cjs +51 -2
- package/dist/objUtils.d.cts +8 -3
- package/dist/objUtils.d.ts +8 -3
- package/dist/objUtils.js +11 -3
- package/dist/parallelAsyncCalls.cjs +2 -1
- package/dist/parallelAsyncCalls.d.cts +5 -4
- package/dist/parallelAsyncCalls.d.ts +5 -4
- package/dist/parallelAsyncCalls.js +2 -1
- package/dist/retryOnError.d.cts +23 -20
- package/dist/retryOnError.d.ts +23 -20
- package/dist/runShellCmd.d.cts +40 -40
- package/dist/runShellCmd.d.ts +40 -40
- package/dist/safeJson.d.cts +6 -2
- package/dist/safeJson.d.ts +6 -2
- package/dist/saferTyping.d.cts +21 -14
- package/dist/saferTyping.d.ts +21 -14
- package/dist/shallowEqual.d.cts +1 -1
- package/dist/shallowEqual.d.ts +1 -1
- package/dist/stringUtils.cjs +2 -2
- package/dist/stringUtils.d.cts +6 -7
- package/dist/stringUtils.d.ts +6 -7
- package/dist/stringUtils.js +1 -1
- package/dist/testUtils.cjs +11 -9
- package/dist/testUtils.d.cts +32 -14
- package/dist/testUtils.d.ts +32 -14
- package/dist/testUtils.js +4 -3
- package/dist/throttle.d.cts +57 -48
- package/dist/throttle.d.ts +57 -48
- package/dist/timers.d.cts +68 -63
- package/dist/timers.d.ts +68 -63
- package/dist/tsResult.d.cts +7 -6
- package/dist/tsResult.d.ts +7 -6
- package/dist/typeGuards.d.cts +65 -64
- package/dist/typeGuards.d.ts +65 -64
- package/dist/typeUtils.d.cts +18 -4
- package/dist/typeUtils.d.ts +18 -4
- package/dist/typedStrings.d.cts +68 -57
- package/dist/typedStrings.d.ts +68 -57
- package/dist/typingFnUtils.d.cts +12 -6
- package/dist/typingFnUtils.d.ts +12 -6
- package/dist/typingFnUtils.js +12 -35
- package/dist/typingTestUtils.cjs +7 -1
- package/dist/typingTestUtils.d.cts +52 -11
- package/dist/typingTestUtils.d.ts +52 -11
- package/dist/typingTestUtils.js +7 -1
- package/dist/typingUtils.d.cts +4 -1
- package/dist/typingUtils.d.ts +4 -1
- package/dist/yamlStringify.cjs +11 -9
- package/dist/yamlStringify.js +2 -2
- package/package.json +5 -1
- package/docs/README.md +0 -72
- package/docs/_media/modules.md +0 -58
- package/docs/arrayUtils/-internal-.md +0 -179
- package/docs/arrayUtils/README.md +0 -550
- package/docs/assertions/-internal-.md +0 -63
- package/docs/assertions/README.md +0 -565
- package/docs/asyncQueue/-internal-.md +0 -297
- package/docs/asyncQueue/README.md +0 -1485
- package/docs/awaitDebounce.md +0 -66
- package/docs/cache/-internal-.md +0 -168
- package/docs/cache/README.md +0 -360
- package/docs/castValues.md +0 -47
- package/docs/concurrentCalls/-internal-.md +0 -490
- package/docs/concurrentCalls/README.md +0 -299
- package/docs/consoleFmt.md +0 -115
- package/docs/conversions.md +0 -27
- package/docs/createThrottleController/-internal-.md +0 -73
- package/docs/createThrottleController/README.md +0 -31
- package/docs/debounce.md +0 -188
- package/docs/dedent/-internal-.md +0 -17
- package/docs/dedent/README.md +0 -204
- package/docs/deepEqual.md +0 -94
- package/docs/enhancedMap.md +0 -358
- package/docs/exhaustiveMatch/-internal-.md +0 -39
- package/docs/exhaustiveMatch/README.md +0 -146
- package/docs/filterObjectOrArrayKeys.md +0 -109
- package/docs/getAutoIncrementId.md +0 -93
- package/docs/getCompositeKey.md +0 -39
- package/docs/getValueStableKey.md +0 -57
- package/docs/hash.md +0 -31
- package/docs/interpolate/-internal-.md +0 -61
- package/docs/interpolate/README.md +0 -62
- package/docs/keepPrevIfUnchanged.md +0 -43
- package/docs/levenshtein.md +0 -93
- package/docs/main.md +0 -21
- package/docs/mathUtils.md +0 -137
- package/docs/modules.md +0 -58
- package/docs/mutationUtils.md +0 -44
- package/docs/objUtils.md +0 -237
- package/docs/parallelAsyncCalls/-internal-.md +0 -347
- package/docs/parallelAsyncCalls/README.md +0 -45
- package/docs/promiseUtils/-internal-.md +0 -69
- package/docs/promiseUtils/README.md +0 -31
- package/docs/retryOnError/-internal-.md +0 -111
- package/docs/retryOnError/README.md +0 -168
- package/docs/runShellCmd/-internal-.md +0 -111
- package/docs/runShellCmd/README.md +0 -201
- package/docs/safeJson.md +0 -51
- package/docs/saferTyping.md +0 -228
- package/docs/serializeXML.md +0 -100
- package/docs/shallowEqual.md +0 -33
- package/docs/sleep.md +0 -27
- package/docs/stringUtils/-internal-.md +0 -17
- package/docs/stringUtils/README.md +0 -270
- package/docs/testUtils.md +0 -382
- package/docs/throttle/-internal-.md +0 -47
- package/docs/throttle/README.md +0 -178
- package/docs/time.md +0 -274
- package/docs/timers.md +0 -256
- package/docs/tsResult/-internal-.md +0 -327
- package/docs/tsResult/README.md +0 -702
- package/docs/typeGuards.md +0 -399
- package/docs/typeUtils/-internal-.md +0 -99
- package/docs/typeUtils/README.md +0 -195
- package/docs/typeUtils.typesTest.md +0 -7
- package/docs/typedStrings.md +0 -458
- package/docs/typingFnUtils/-internal-.md +0 -43
- package/docs/typingFnUtils/README.md +0 -317
- package/docs/typingTestUtils.md +0 -172
- package/docs/typingUtils.md +0 -135
- package/docs/yamlStringify.md +0 -83
|
@@ -1,565 +0,0 @@
|
|
|
1
|
-
[**@ls-stack/utils**](../README.md)
|
|
2
|
-
|
|
3
|
-
***
|
|
4
|
-
|
|
5
|
-
[@ls-stack/utils](../modules.md) / assertions
|
|
6
|
-
|
|
7
|
-
# assertions
|
|
8
|
-
|
|
9
|
-
## Modules
|
|
10
|
-
|
|
11
|
-
- [\<internal\>](-internal-.md)
|
|
12
|
-
|
|
13
|
-
## Variables
|
|
14
|
-
|
|
15
|
-
### ~~isFunction()~~
|
|
16
|
-
|
|
17
|
-
```ts
|
|
18
|
-
const isFunction: (value) => value is (args: any[]) => any = isFunctionFromTypeGuards;
|
|
19
|
-
```
|
|
20
|
-
|
|
21
|
-
Defined in: [packages/utils/src/assertions.ts:240](https://github.com/lucasols/utils/blob/main/packages/utils/src/assertions.ts#L240)
|
|
22
|
-
|
|
23
|
-
Type guard to check if a value is a function.
|
|
24
|
-
|
|
25
|
-
Returns true if the value is a function of any kind (regular function,
|
|
26
|
-
arrow function, method, constructor, etc.).
|
|
27
|
-
|
|
28
|
-
#### Parameters
|
|
29
|
-
|
|
30
|
-
##### value
|
|
31
|
-
|
|
32
|
-
`unknown`
|
|
33
|
-
|
|
34
|
-
The value to check
|
|
35
|
-
|
|
36
|
-
#### Returns
|
|
37
|
-
|
|
38
|
-
`value is (args: any[]) => any`
|
|
39
|
-
|
|
40
|
-
True if the value is a function, false otherwise
|
|
41
|
-
|
|
42
|
-
#### Example
|
|
43
|
-
|
|
44
|
-
```typescript
|
|
45
|
-
if (isFunction(value)) {
|
|
46
|
-
// TypeScript knows value is (...args: any[]) => any
|
|
47
|
-
const result = value();
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
isFunction(() => {}); // true
|
|
51
|
-
isFunction(function() {}); // true
|
|
52
|
-
isFunction(Math.max); // true
|
|
53
|
-
isFunction('string'); // false
|
|
54
|
-
isFunction({}); // false
|
|
55
|
-
```
|
|
56
|
-
|
|
57
|
-
#### Deprecated
|
|
58
|
-
|
|
59
|
-
use import from `@ls-stack/typeGuards` instead
|
|
60
|
-
|
|
61
|
-
***
|
|
62
|
-
|
|
63
|
-
### ~~isObject()~~
|
|
64
|
-
|
|
65
|
-
```ts
|
|
66
|
-
const isObject: (value) => value is Record<string, unknown> = isObjectFromTypeGuards;
|
|
67
|
-
```
|
|
68
|
-
|
|
69
|
-
Defined in: [packages/utils/src/assertions.ts:242](https://github.com/lucasols/utils/blob/main/packages/utils/src/assertions.ts#L242)
|
|
70
|
-
|
|
71
|
-
Type guard to check if a value is a plain object (not null, not an array).
|
|
72
|
-
|
|
73
|
-
Returns true if the value is an object that is not null and not an array.
|
|
74
|
-
This is useful for distinguishing between objects and other types.
|
|
75
|
-
|
|
76
|
-
#### Parameters
|
|
77
|
-
|
|
78
|
-
##### value
|
|
79
|
-
|
|
80
|
-
`unknown`
|
|
81
|
-
|
|
82
|
-
The value to check
|
|
83
|
-
|
|
84
|
-
#### Returns
|
|
85
|
-
|
|
86
|
-
`value is Record<string, unknown>`
|
|
87
|
-
|
|
88
|
-
True if the value is a plain object, false otherwise
|
|
89
|
-
|
|
90
|
-
#### Example
|
|
91
|
-
|
|
92
|
-
```typescript
|
|
93
|
-
if (isObject(value)) {
|
|
94
|
-
// TypeScript knows value is Record<string, unknown>
|
|
95
|
-
console.log(value.someProperty);
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
isObject({}); // true
|
|
99
|
-
isObject({ a: 1 }); // true
|
|
100
|
-
isObject(null); // false
|
|
101
|
-
isObject([]); // false
|
|
102
|
-
isObject('string'); // false
|
|
103
|
-
```
|
|
104
|
-
|
|
105
|
-
#### Deprecated
|
|
106
|
-
|
|
107
|
-
use import from `@ls-stack/typeGuards` instead
|
|
108
|
-
|
|
109
|
-
***
|
|
110
|
-
|
|
111
|
-
### ~~isPlainObject()~~
|
|
112
|
-
|
|
113
|
-
```ts
|
|
114
|
-
const isPlainObject: (value) => value is Record<string, unknown> = isPlainObjectFromTypeGuards;
|
|
115
|
-
```
|
|
116
|
-
|
|
117
|
-
Defined in: [packages/utils/src/assertions.ts:244](https://github.com/lucasols/utils/blob/main/packages/utils/src/assertions.ts#L244)
|
|
118
|
-
|
|
119
|
-
Type guard to check if a value is a plain object (created by Object literal or Object constructor).
|
|
120
|
-
|
|
121
|
-
Returns true if the value is a plain object - an object created by the Object
|
|
122
|
-
constructor or object literal syntax. This excludes instances of classes,
|
|
123
|
-
built-in objects like Date, RegExp, etc.
|
|
124
|
-
|
|
125
|
-
#### Parameters
|
|
126
|
-
|
|
127
|
-
##### value
|
|
128
|
-
|
|
129
|
-
`any`
|
|
130
|
-
|
|
131
|
-
The value to check
|
|
132
|
-
|
|
133
|
-
#### Returns
|
|
134
|
-
|
|
135
|
-
`value is Record<string, unknown>`
|
|
136
|
-
|
|
137
|
-
True if the value is a plain object, false otherwise
|
|
138
|
-
|
|
139
|
-
#### Example
|
|
140
|
-
|
|
141
|
-
```typescript
|
|
142
|
-
if (isPlainObject(value)) {
|
|
143
|
-
// TypeScript knows value is Record<string, unknown>
|
|
144
|
-
console.log(Object.keys(value));
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
isPlainObject({}); // true
|
|
148
|
-
isPlainObject({ a: 1 }); // true
|
|
149
|
-
isPlainObject(Object.create(null)); // true
|
|
150
|
-
isPlainObject(new Date()); // false
|
|
151
|
-
isPlainObject(/regex/); // false
|
|
152
|
-
isPlainObject(new MyClass()); // false
|
|
153
|
-
isPlainObject([]); // false
|
|
154
|
-
```
|
|
155
|
-
|
|
156
|
-
#### Deprecated
|
|
157
|
-
|
|
158
|
-
use import from `@ls-stack/typeGuards` instead
|
|
159
|
-
|
|
160
|
-
***
|
|
161
|
-
|
|
162
|
-
### ~~isPromise()~~
|
|
163
|
-
|
|
164
|
-
```ts
|
|
165
|
-
const isPromise: (value) => value is Promise<unknown> = isPromiseFromTypeGuards;
|
|
166
|
-
```
|
|
167
|
-
|
|
168
|
-
Defined in: [packages/utils/src/assertions.ts:246](https://github.com/lucasols/utils/blob/main/packages/utils/src/assertions.ts#L246)
|
|
169
|
-
|
|
170
|
-
Type guard to check if a value is a Promise or thenable object.
|
|
171
|
-
|
|
172
|
-
Returns true if the value is an object with a `then` method that is a function.
|
|
173
|
-
This covers both native Promises and thenable objects that implement the
|
|
174
|
-
Promise interface.
|
|
175
|
-
|
|
176
|
-
#### Parameters
|
|
177
|
-
|
|
178
|
-
##### value
|
|
179
|
-
|
|
180
|
-
`unknown`
|
|
181
|
-
|
|
182
|
-
The value to check
|
|
183
|
-
|
|
184
|
-
#### Returns
|
|
185
|
-
|
|
186
|
-
`value is Promise<unknown>`
|
|
187
|
-
|
|
188
|
-
True if the value is a Promise or thenable, false otherwise
|
|
189
|
-
|
|
190
|
-
#### Example
|
|
191
|
-
|
|
192
|
-
```typescript
|
|
193
|
-
if (isPromise(value)) {
|
|
194
|
-
// TypeScript knows value is Promise<unknown>
|
|
195
|
-
const result = await value;
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
isPromise(Promise.resolve()); // true
|
|
199
|
-
isPromise(new Promise(() => {})); // true
|
|
200
|
-
isPromise({ then: () => {} }); // true
|
|
201
|
-
isPromise({ then: 'not a function' }); // false
|
|
202
|
-
isPromise('string'); // false
|
|
203
|
-
```
|
|
204
|
-
|
|
205
|
-
#### Deprecated
|
|
206
|
-
|
|
207
|
-
use import from `@ls-stack/typeGuards` instead
|
|
208
|
-
|
|
209
|
-
## Functions
|
|
210
|
-
|
|
211
|
-
### assertIsNotNullish()
|
|
212
|
-
|
|
213
|
-
```ts
|
|
214
|
-
function assertIsNotNullish<T>(value, error): asserts value is StrictNonNullable<T, never>;
|
|
215
|
-
```
|
|
216
|
-
|
|
217
|
-
Defined in: [packages/utils/src/assertions.ts:111](https://github.com/lucasols/utils/blob/main/packages/utils/src/assertions.ts#L111)
|
|
218
|
-
|
|
219
|
-
Asserts that a value is not null or undefined using TypeScript's assertion signature.
|
|
220
|
-
|
|
221
|
-
Throws an error if the value is null or undefined. Use it instead of `!` operator for better type safety.
|
|
222
|
-
|
|
223
|
-
#### Type Parameters
|
|
224
|
-
|
|
225
|
-
##### T
|
|
226
|
-
|
|
227
|
-
`T`
|
|
228
|
-
|
|
229
|
-
The type of the input value
|
|
230
|
-
|
|
231
|
-
#### Parameters
|
|
232
|
-
|
|
233
|
-
##### value
|
|
234
|
-
|
|
235
|
-
`T`
|
|
236
|
-
|
|
237
|
-
The value to assert is not null or undefined
|
|
238
|
-
|
|
239
|
-
##### error
|
|
240
|
-
|
|
241
|
-
Error message string or function that returns an Error to throw if value is nullish
|
|
242
|
-
|
|
243
|
-
`string` | () => `Error`
|
|
244
|
-
|
|
245
|
-
#### Returns
|
|
246
|
-
|
|
247
|
-
`asserts value is StrictNonNullable<T, never>`
|
|
248
|
-
|
|
249
|
-
#### Throws
|
|
250
|
-
|
|
251
|
-
When the value is null or undefined
|
|
252
|
-
|
|
253
|
-
#### Example
|
|
254
|
-
|
|
255
|
-
```typescript
|
|
256
|
-
function processValue(input: string | null | undefined) {
|
|
257
|
-
assertIsNotNullish(input);
|
|
258
|
-
// TypeScript now knows input is string
|
|
259
|
-
console.log(input.toUpperCase());
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
// With custom error
|
|
263
|
-
assertIsNotNullish(value, 'Value is required for processing');
|
|
264
|
-
```
|
|
265
|
-
|
|
266
|
-
***
|
|
267
|
-
|
|
268
|
-
### assertIsNotUndefined()
|
|
269
|
-
|
|
270
|
-
```ts
|
|
271
|
-
function assertIsNotUndefined<T>(value, error): asserts value is StrictNonUndefined<T, never>;
|
|
272
|
-
```
|
|
273
|
-
|
|
274
|
-
Defined in: [packages/utils/src/assertions.ts:142](https://github.com/lucasols/utils/blob/main/packages/utils/src/assertions.ts#L142)
|
|
275
|
-
|
|
276
|
-
Asserts that a value is not undefined using TypeScript's assertion signature.
|
|
277
|
-
|
|
278
|
-
Throws an error if the value is undefined. Use it instead of `!` operator for better type safety.
|
|
279
|
-
|
|
280
|
-
#### Type Parameters
|
|
281
|
-
|
|
282
|
-
##### T
|
|
283
|
-
|
|
284
|
-
`T`
|
|
285
|
-
|
|
286
|
-
The type of the input value
|
|
287
|
-
|
|
288
|
-
#### Parameters
|
|
289
|
-
|
|
290
|
-
##### value
|
|
291
|
-
|
|
292
|
-
`T`
|
|
293
|
-
|
|
294
|
-
The value to assert is not undefined
|
|
295
|
-
|
|
296
|
-
##### error
|
|
297
|
-
|
|
298
|
-
Error message string or function that returns an Error to throw if value is undefined
|
|
299
|
-
|
|
300
|
-
`string` | () => `Error`
|
|
301
|
-
|
|
302
|
-
#### Returns
|
|
303
|
-
|
|
304
|
-
`asserts value is StrictNonUndefined<T, never>`
|
|
305
|
-
|
|
306
|
-
#### Throws
|
|
307
|
-
|
|
308
|
-
When the value is undefined
|
|
309
|
-
|
|
310
|
-
#### Example
|
|
311
|
-
|
|
312
|
-
```typescript
|
|
313
|
-
function processValue(input: string | undefined) {
|
|
314
|
-
assertIsNotUndefined(input);
|
|
315
|
-
// TypeScript now knows input is string
|
|
316
|
-
console.log(input.toUpperCase());
|
|
317
|
-
}
|
|
318
|
-
|
|
319
|
-
// With custom error
|
|
320
|
-
assertIsNotUndefined(value, 'Value must be defined');
|
|
321
|
-
```
|
|
322
|
-
|
|
323
|
-
***
|
|
324
|
-
|
|
325
|
-
### exhaustiveCheck()
|
|
326
|
-
|
|
327
|
-
```ts
|
|
328
|
-
function exhaustiveCheck<Except>(narrowedType): Error;
|
|
329
|
-
```
|
|
330
|
-
|
|
331
|
-
Defined in: [packages/utils/src/assertions.ts:232](https://github.com/lucasols/utils/blob/main/packages/utils/src/assertions.ts#L232)
|
|
332
|
-
|
|
333
|
-
Ensures exhaustive type checking in switch statements or conditional logic.
|
|
334
|
-
|
|
335
|
-
This function should be used in the default case of switch statements or
|
|
336
|
-
the final else branch of conditional logic to ensure all possible cases
|
|
337
|
-
are handled. It helps catch missing cases at compile time when new union
|
|
338
|
-
members are added.
|
|
339
|
-
|
|
340
|
-
#### Type Parameters
|
|
341
|
-
|
|
342
|
-
##### Except
|
|
343
|
-
|
|
344
|
-
`Except` = `never`
|
|
345
|
-
|
|
346
|
-
The type that should never be reached
|
|
347
|
-
|
|
348
|
-
#### Parameters
|
|
349
|
-
|
|
350
|
-
##### narrowedType
|
|
351
|
-
|
|
352
|
-
`NoInfer`\<`Except`\>
|
|
353
|
-
|
|
354
|
-
The value that should never exist at runtime
|
|
355
|
-
|
|
356
|
-
#### Returns
|
|
357
|
-
|
|
358
|
-
`Error`
|
|
359
|
-
|
|
360
|
-
An Error object (this function should never actually execute)
|
|
361
|
-
|
|
362
|
-
#### Example
|
|
363
|
-
|
|
364
|
-
```typescript
|
|
365
|
-
type Status = 'pending' | 'success' | 'error';
|
|
366
|
-
|
|
367
|
-
function handleStatus(status: Status) {
|
|
368
|
-
switch (status) {
|
|
369
|
-
case 'pending':
|
|
370
|
-
return 'Loading...';
|
|
371
|
-
case 'success':
|
|
372
|
-
return 'Done!';
|
|
373
|
-
case 'error':
|
|
374
|
-
return 'Failed!';
|
|
375
|
-
default:
|
|
376
|
-
throw exhaustiveCheck(status); // TypeScript error if Status gains new members
|
|
377
|
-
}
|
|
378
|
-
}
|
|
379
|
-
|
|
380
|
-
// In conditional logic
|
|
381
|
-
function processValue(value: string | number) {
|
|
382
|
-
if (typeof value === 'string') {
|
|
383
|
-
return value.toUpperCase();
|
|
384
|
-
} else if (typeof value === 'number') {
|
|
385
|
-
return value.toString();
|
|
386
|
-
} else {
|
|
387
|
-
throw exhaustiveCheck(value); // Ensures all cases are covered
|
|
388
|
-
}
|
|
389
|
-
}
|
|
390
|
-
```
|
|
391
|
-
|
|
392
|
-
***
|
|
393
|
-
|
|
394
|
-
### invariant()
|
|
395
|
-
|
|
396
|
-
```ts
|
|
397
|
-
function invariant(condition, error): asserts condition;
|
|
398
|
-
```
|
|
399
|
-
|
|
400
|
-
Defined in: [packages/utils/src/assertions.ts:180](https://github.com/lucasols/utils/blob/main/packages/utils/src/assertions.ts#L180)
|
|
401
|
-
|
|
402
|
-
Asserts that a condition is always true, throwing an error if it's falsy.
|
|
403
|
-
|
|
404
|
-
This function is useful for enforcing invariants in your code - conditions that
|
|
405
|
-
should always be true. It uses TypeScript's assertion signature to narrow types
|
|
406
|
-
based on the condition.
|
|
407
|
-
|
|
408
|
-
#### Parameters
|
|
409
|
-
|
|
410
|
-
##### condition
|
|
411
|
-
|
|
412
|
-
`any`
|
|
413
|
-
|
|
414
|
-
The condition to check (any truthy/falsy value)
|
|
415
|
-
|
|
416
|
-
##### error
|
|
417
|
-
|
|
418
|
-
Error message string or function that returns an Error to throw if condition is falsy
|
|
419
|
-
|
|
420
|
-
`string` | () => `Error`
|
|
421
|
-
|
|
422
|
-
#### Returns
|
|
423
|
-
|
|
424
|
-
`asserts condition`
|
|
425
|
-
|
|
426
|
-
#### Throws
|
|
427
|
-
|
|
428
|
-
When the condition is falsy
|
|
429
|
-
|
|
430
|
-
#### Example
|
|
431
|
-
|
|
432
|
-
```typescript
|
|
433
|
-
function divide(a: number, b: number) {
|
|
434
|
-
invariant(b !== 0, 'Division by zero is not allowed');
|
|
435
|
-
return a / b;
|
|
436
|
-
}
|
|
437
|
-
|
|
438
|
-
// Type narrowing example
|
|
439
|
-
function processUser(user: User | null) {
|
|
440
|
-
invariant(user, 'User must be logged in');
|
|
441
|
-
// TypeScript now knows user is User, not null
|
|
442
|
-
console.log(user.name);
|
|
443
|
-
}
|
|
444
|
-
|
|
445
|
-
// With custom error function
|
|
446
|
-
invariant(isValid, () => new ValidationError('Invalid state detected'));
|
|
447
|
-
```
|
|
448
|
-
|
|
449
|
-
***
|
|
450
|
-
|
|
451
|
-
### notNullish()
|
|
452
|
-
|
|
453
|
-
```ts
|
|
454
|
-
function notNullish<T>(value, error): StrictNonNullable<T>;
|
|
455
|
-
```
|
|
456
|
-
|
|
457
|
-
Defined in: [packages/utils/src/assertions.ts:78](https://github.com/lucasols/utils/blob/main/packages/utils/src/assertions.ts#L78)
|
|
458
|
-
|
|
459
|
-
Ensures a value is not null or undefined and returns it with the correct type.
|
|
460
|
-
|
|
461
|
-
Throws an error if the value is null or undefined. Use it instead of `!` operator for better type safety.
|
|
462
|
-
|
|
463
|
-
#### Type Parameters
|
|
464
|
-
|
|
465
|
-
##### T
|
|
466
|
-
|
|
467
|
-
`T`
|
|
468
|
-
|
|
469
|
-
The type of the input value
|
|
470
|
-
|
|
471
|
-
#### Parameters
|
|
472
|
-
|
|
473
|
-
##### value
|
|
474
|
-
|
|
475
|
-
`T`
|
|
476
|
-
|
|
477
|
-
The value to check for null or undefined
|
|
478
|
-
|
|
479
|
-
##### error
|
|
480
|
-
|
|
481
|
-
Error message string or function that returns an Error to throw if value is nullish
|
|
482
|
-
|
|
483
|
-
`string` | () => `Error`
|
|
484
|
-
|
|
485
|
-
#### Returns
|
|
486
|
-
|
|
487
|
-
[`StrictNonNullable`](-internal-.md#strictnonnullable)\<`T`\>
|
|
488
|
-
|
|
489
|
-
The input value with null and undefined excluded from its type
|
|
490
|
-
|
|
491
|
-
#### Throws
|
|
492
|
-
|
|
493
|
-
When the value is null or undefined
|
|
494
|
-
|
|
495
|
-
#### Example
|
|
496
|
-
|
|
497
|
-
```typescript
|
|
498
|
-
const maybeString: string | null | undefined = getValue();
|
|
499
|
-
const definiteString = notNullish(maybeString); // Type: string
|
|
500
|
-
|
|
501
|
-
// With custom error message
|
|
502
|
-
const value = notNullish(maybeValue, 'Value cannot be null or undefined');
|
|
503
|
-
|
|
504
|
-
// With custom error function
|
|
505
|
-
const value = notNullish(maybeValue, () => new ValidationError('Required field'));
|
|
506
|
-
```
|
|
507
|
-
|
|
508
|
-
***
|
|
509
|
-
|
|
510
|
-
### notUndefined()
|
|
511
|
-
|
|
512
|
-
```ts
|
|
513
|
-
function notUndefined<T>(value, error): StrictNonUndefined<T>;
|
|
514
|
-
```
|
|
515
|
-
|
|
516
|
-
Defined in: [packages/utils/src/assertions.ts:39](https://github.com/lucasols/utils/blob/main/packages/utils/src/assertions.ts#L39)
|
|
517
|
-
|
|
518
|
-
Ensures a value is not undefined and returns it with the correct type.
|
|
519
|
-
|
|
520
|
-
Throws an error if the value is undefined. Use it instead of `!` operator for better type safety.
|
|
521
|
-
|
|
522
|
-
#### Type Parameters
|
|
523
|
-
|
|
524
|
-
##### T
|
|
525
|
-
|
|
526
|
-
`T`
|
|
527
|
-
|
|
528
|
-
The type of the input value
|
|
529
|
-
|
|
530
|
-
#### Parameters
|
|
531
|
-
|
|
532
|
-
##### value
|
|
533
|
-
|
|
534
|
-
`T`
|
|
535
|
-
|
|
536
|
-
The value to check for undefined
|
|
537
|
-
|
|
538
|
-
##### error
|
|
539
|
-
|
|
540
|
-
Error message string or function that returns an Error to throw if value is undefined
|
|
541
|
-
|
|
542
|
-
`string` | () => `Error`
|
|
543
|
-
|
|
544
|
-
#### Returns
|
|
545
|
-
|
|
546
|
-
[`StrictNonUndefined`](-internal-.md#strictnonundefined)\<`T`\>
|
|
547
|
-
|
|
548
|
-
The input value with undefined excluded from its type
|
|
549
|
-
|
|
550
|
-
#### Throws
|
|
551
|
-
|
|
552
|
-
When the value is undefined
|
|
553
|
-
|
|
554
|
-
#### Example
|
|
555
|
-
|
|
556
|
-
```typescript
|
|
557
|
-
const maybeString: string | undefined = getValue();
|
|
558
|
-
const definiteString = notUndefined(maybeString); // Type: string
|
|
559
|
-
|
|
560
|
-
// With custom error message
|
|
561
|
-
const value = notUndefined(maybeValue, 'Value must be defined');
|
|
562
|
-
|
|
563
|
-
// With custom error function
|
|
564
|
-
const value = notUndefined(maybeValue, () => new ValidationError('Required field'));
|
|
565
|
-
```
|