ts-data-forge 1.0.0 → 1.0.1
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 +1 -1
- package/dist/array/array-utils.d.mts +2617 -0
- package/dist/array/array-utils.d.mts.map +1 -0
- package/dist/array/array-utils.mjs +2915 -0
- package/dist/array/array-utils.mjs.map +1 -0
- package/dist/array/index.d.mts +3 -0
- package/dist/array/index.d.mts.map +1 -0
- package/dist/array/index.mjs +3 -0
- package/dist/array/index.mjs.map +1 -0
- package/dist/array/tuple-utils.d.mts +421 -0
- package/dist/array/tuple-utils.d.mts.map +1 -0
- package/dist/array/tuple-utils.mjs +391 -0
- package/dist/array/tuple-utils.mjs.map +1 -0
- package/dist/collections/imap-mapped.d.mts +445 -0
- package/dist/collections/imap-mapped.d.mts.map +1 -0
- package/dist/collections/imap-mapped.mjs +424 -0
- package/dist/collections/imap-mapped.mjs.map +1 -0
- package/dist/collections/imap.d.mts +359 -0
- package/dist/collections/imap.d.mts.map +1 -0
- package/dist/collections/imap.mjs +338 -0
- package/dist/collections/imap.mjs.map +1 -0
- package/dist/collections/index.d.mts +7 -0
- package/dist/collections/index.d.mts.map +1 -0
- package/dist/collections/index.mjs +7 -0
- package/dist/collections/index.mjs.map +1 -0
- package/dist/collections/iset-mapped.d.mts +576 -0
- package/dist/collections/iset-mapped.d.mts.map +1 -0
- package/dist/collections/iset-mapped.mjs +522 -0
- package/dist/collections/iset-mapped.mjs.map +1 -0
- package/dist/collections/iset.d.mts +426 -0
- package/dist/collections/iset.d.mts.map +1 -0
- package/dist/collections/iset.mjs +437 -0
- package/dist/collections/iset.mjs.map +1 -0
- package/dist/collections/queue.d.mts +190 -0
- package/dist/collections/queue.d.mts.map +1 -0
- package/dist/collections/queue.mjs +317 -0
- package/dist/collections/queue.mjs.map +1 -0
- package/dist/collections/stack.d.mts +210 -0
- package/dist/collections/stack.d.mts.map +1 -0
- package/dist/collections/stack.mjs +353 -0
- package/dist/collections/stack.mjs.map +1 -0
- package/dist/expect-type.d.mts +199 -0
- package/dist/expect-type.d.mts.map +1 -0
- package/dist/expect-type.mjs +201 -0
- package/dist/expect-type.mjs.map +1 -0
- package/dist/functional/index.d.mts +5 -0
- package/dist/functional/index.d.mts.map +1 -0
- package/dist/functional/index.mjs +5 -0
- package/dist/functional/index.mjs.map +1 -0
- package/dist/functional/match.d.mts +215 -0
- package/dist/functional/match.d.mts.map +1 -0
- package/dist/functional/match.mjs +139 -0
- package/dist/functional/match.mjs.map +1 -0
- package/dist/functional/optional.d.mts +517 -0
- package/dist/functional/optional.d.mts.map +1 -0
- package/dist/functional/optional.mjs +532 -0
- package/dist/functional/optional.mjs.map +1 -0
- package/dist/functional/pipe.d.mts +185 -0
- package/dist/functional/pipe.d.mts.map +1 -0
- package/dist/functional/pipe.mjs +129 -0
- package/dist/functional/pipe.mjs.map +1 -0
- package/dist/functional/result.d.mts +796 -0
- package/dist/functional/result.d.mts.map +1 -0
- package/dist/functional/result.mjs +844 -0
- package/dist/functional/result.mjs.map +1 -0
- package/dist/globals.d.mts +38 -0
- package/dist/guard/has-key.d.mts +100 -0
- package/dist/guard/has-key.d.mts.map +1 -0
- package/dist/guard/has-key.mjs +94 -0
- package/dist/guard/has-key.mjs.map +1 -0
- package/dist/guard/index.d.mts +8 -0
- package/dist/guard/index.d.mts.map +1 -0
- package/dist/guard/index.mjs +8 -0
- package/dist/guard/index.mjs.map +1 -0
- package/dist/guard/is-non-empty-string.d.mts +106 -0
- package/dist/guard/is-non-empty-string.d.mts.map +1 -0
- package/dist/guard/is-non-empty-string.mjs +108 -0
- package/dist/guard/is-non-empty-string.mjs.map +1 -0
- package/dist/guard/is-non-null-object.d.mts +105 -0
- package/dist/guard/is-non-null-object.d.mts.map +1 -0
- package/dist/guard/is-non-null-object.mjs +108 -0
- package/dist/guard/is-non-null-object.mjs.map +1 -0
- package/dist/guard/is-primitive.d.mts +146 -0
- package/dist/guard/is-primitive.d.mts.map +1 -0
- package/dist/guard/is-primitive.mjs +161 -0
- package/dist/guard/is-primitive.mjs.map +1 -0
- package/dist/guard/is-record.d.mts +151 -0
- package/dist/guard/is-record.d.mts.map +1 -0
- package/dist/guard/is-record.mjs +155 -0
- package/dist/guard/is-record.mjs.map +1 -0
- package/dist/guard/is-type.d.mts +430 -0
- package/dist/guard/is-type.d.mts.map +1 -0
- package/dist/guard/is-type.mjs +432 -0
- package/dist/guard/is-type.mjs.map +1 -0
- package/dist/guard/key-is-in.d.mts +158 -0
- package/dist/guard/key-is-in.d.mts.map +1 -0
- package/dist/guard/key-is-in.mjs +160 -0
- package/dist/guard/key-is-in.mjs.map +1 -0
- package/dist/index.d.mts +11 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +61 -0
- package/dist/index.mjs.map +1 -0
- package/dist/iterator/index.d.mts +2 -0
- package/dist/iterator/index.d.mts.map +1 -0
- package/dist/iterator/index.mjs +2 -0
- package/dist/iterator/index.mjs.map +1 -0
- package/dist/iterator/range.d.mts +97 -0
- package/dist/iterator/range.d.mts.map +1 -0
- package/dist/iterator/range.mjs +130 -0
- package/dist/iterator/range.mjs.map +1 -0
- package/dist/json/index.d.mts +2 -0
- package/dist/json/index.d.mts.map +1 -0
- package/dist/json/index.mjs +2 -0
- package/dist/json/index.mjs.map +1 -0
- package/dist/json/json.d.mts +597 -0
- package/dist/json/json.d.mts.map +1 -0
- package/dist/json/json.mjs +687 -0
- package/dist/json/json.mjs.map +1 -0
- package/dist/number/branded-types/finite-number.d.mts +291 -0
- package/dist/number/branded-types/finite-number.d.mts.map +1 -0
- package/dist/number/branded-types/finite-number.mjs +296 -0
- package/dist/number/branded-types/finite-number.mjs.map +1 -0
- package/dist/number/branded-types/index.d.mts +27 -0
- package/dist/number/branded-types/index.d.mts.map +1 -0
- package/dist/number/branded-types/index.mjs +27 -0
- package/dist/number/branded-types/index.mjs.map +1 -0
- package/dist/number/branded-types/int.d.mts +242 -0
- package/dist/number/branded-types/int.d.mts.map +1 -0
- package/dist/number/branded-types/int.mjs +239 -0
- package/dist/number/branded-types/int.mjs.map +1 -0
- package/dist/number/branded-types/int16.d.mts +162 -0
- package/dist/number/branded-types/int16.d.mts.map +1 -0
- package/dist/number/branded-types/int16.mjs +141 -0
- package/dist/number/branded-types/int16.mjs.map +1 -0
- package/dist/number/branded-types/int32.d.mts +155 -0
- package/dist/number/branded-types/int32.d.mts.map +1 -0
- package/dist/number/branded-types/int32.mjs +142 -0
- package/dist/number/branded-types/int32.mjs.map +1 -0
- package/dist/number/branded-types/non-negative-finite-number.d.mts +165 -0
- package/dist/number/branded-types/non-negative-finite-number.d.mts.map +1 -0
- package/dist/number/branded-types/non-negative-finite-number.mjs +160 -0
- package/dist/number/branded-types/non-negative-finite-number.mjs.map +1 -0
- package/dist/number/branded-types/non-negative-int16.d.mts +160 -0
- package/dist/number/branded-types/non-negative-int16.d.mts.map +1 -0
- package/dist/number/branded-types/non-negative-int16.mjs +138 -0
- package/dist/number/branded-types/non-negative-int16.mjs.map +1 -0
- package/dist/number/branded-types/non-negative-int32.d.mts +156 -0
- package/dist/number/branded-types/non-negative-int32.d.mts.map +1 -0
- package/dist/number/branded-types/non-negative-int32.mjs +138 -0
- package/dist/number/branded-types/non-negative-int32.mjs.map +1 -0
- package/dist/number/branded-types/non-zero-finite-number.d.mts +154 -0
- package/dist/number/branded-types/non-zero-finite-number.d.mts.map +1 -0
- package/dist/number/branded-types/non-zero-finite-number.mjs +160 -0
- package/dist/number/branded-types/non-zero-finite-number.mjs.map +1 -0
- package/dist/number/branded-types/non-zero-int.d.mts +131 -0
- package/dist/number/branded-types/non-zero-int.d.mts.map +1 -0
- package/dist/number/branded-types/non-zero-int.mjs +128 -0
- package/dist/number/branded-types/non-zero-int.mjs.map +1 -0
- package/dist/number/branded-types/non-zero-int16.d.mts +166 -0
- package/dist/number/branded-types/non-zero-int16.d.mts.map +1 -0
- package/dist/number/branded-types/non-zero-int16.mjs +145 -0
- package/dist/number/branded-types/non-zero-int16.mjs.map +1 -0
- package/dist/number/branded-types/non-zero-int32.d.mts +158 -0
- package/dist/number/branded-types/non-zero-int32.d.mts.map +1 -0
- package/dist/number/branded-types/non-zero-int32.mjs +145 -0
- package/dist/number/branded-types/non-zero-int32.mjs.map +1 -0
- package/dist/number/branded-types/non-zero-safe-int.d.mts +148 -0
- package/dist/number/branded-types/non-zero-safe-int.d.mts.map +1 -0
- package/dist/number/branded-types/non-zero-safe-int.mjs +145 -0
- package/dist/number/branded-types/non-zero-safe-int.mjs.map +1 -0
- package/dist/number/branded-types/non-zero-uint16.d.mts +160 -0
- package/dist/number/branded-types/non-zero-uint16.d.mts.map +1 -0
- package/dist/number/branded-types/non-zero-uint16.mjs +140 -0
- package/dist/number/branded-types/non-zero-uint16.mjs.map +1 -0
- package/dist/number/branded-types/non-zero-uint32.d.mts +156 -0
- package/dist/number/branded-types/non-zero-uint32.d.mts.map +1 -0
- package/dist/number/branded-types/non-zero-uint32.mjs +140 -0
- package/dist/number/branded-types/non-zero-uint32.mjs.map +1 -0
- package/dist/number/branded-types/positive-finite-number.d.mts +171 -0
- package/dist/number/branded-types/positive-finite-number.d.mts.map +1 -0
- package/dist/number/branded-types/positive-finite-number.mjs +165 -0
- package/dist/number/branded-types/positive-finite-number.mjs.map +1 -0
- package/dist/number/branded-types/positive-int.d.mts +270 -0
- package/dist/number/branded-types/positive-int.d.mts.map +1 -0
- package/dist/number/branded-types/positive-int.mjs +257 -0
- package/dist/number/branded-types/positive-int.mjs.map +1 -0
- package/dist/number/branded-types/positive-int16.d.mts +162 -0
- package/dist/number/branded-types/positive-int16.d.mts.map +1 -0
- package/dist/number/branded-types/positive-int16.mjs +139 -0
- package/dist/number/branded-types/positive-int16.mjs.map +1 -0
- package/dist/number/branded-types/positive-int32.d.mts +158 -0
- package/dist/number/branded-types/positive-int32.d.mts.map +1 -0
- package/dist/number/branded-types/positive-int32.mjs +139 -0
- package/dist/number/branded-types/positive-int32.mjs.map +1 -0
- package/dist/number/branded-types/positive-safe-int.d.mts +152 -0
- package/dist/number/branded-types/positive-safe-int.d.mts.map +1 -0
- package/dist/number/branded-types/positive-safe-int.mjs +138 -0
- package/dist/number/branded-types/positive-safe-int.mjs.map +1 -0
- package/dist/number/branded-types/positive-uint16.d.mts +160 -0
- package/dist/number/branded-types/positive-uint16.d.mts.map +1 -0
- package/dist/number/branded-types/positive-uint16.mjs +139 -0
- package/dist/number/branded-types/positive-uint16.mjs.map +1 -0
- package/dist/number/branded-types/positive-uint32.d.mts +156 -0
- package/dist/number/branded-types/positive-uint32.d.mts.map +1 -0
- package/dist/number/branded-types/positive-uint32.mjs +139 -0
- package/dist/number/branded-types/positive-uint32.mjs.map +1 -0
- package/dist/number/branded-types/safe-int.d.mts +243 -0
- package/dist/number/branded-types/safe-int.d.mts.map +1 -0
- package/dist/number/branded-types/safe-int.mjs +240 -0
- package/dist/number/branded-types/safe-int.mjs.map +1 -0
- package/dist/number/branded-types/safe-uint.d.mts +151 -0
- package/dist/number/branded-types/safe-uint.d.mts.map +1 -0
- package/dist/number/branded-types/safe-uint.mjs +138 -0
- package/dist/number/branded-types/safe-uint.mjs.map +1 -0
- package/dist/number/branded-types/uint.d.mts +144 -0
- package/dist/number/branded-types/uint.d.mts.map +1 -0
- package/dist/number/branded-types/uint.mjs +132 -0
- package/dist/number/branded-types/uint.mjs.map +1 -0
- package/dist/number/branded-types/uint16.d.mts +157 -0
- package/dist/number/branded-types/uint16.d.mts.map +1 -0
- package/dist/number/branded-types/uint16.mjs +137 -0
- package/dist/number/branded-types/uint16.mjs.map +1 -0
- package/dist/number/branded-types/uint32.d.mts +185 -0
- package/dist/number/branded-types/uint32.d.mts.map +1 -0
- package/dist/number/branded-types/uint32.mjs +169 -0
- package/dist/number/branded-types/uint32.mjs.map +1 -0
- package/dist/number/enum/index.d.mts +3 -0
- package/dist/number/enum/index.d.mts.map +1 -0
- package/dist/number/enum/index.mjs +3 -0
- package/dist/number/enum/index.mjs.map +1 -0
- package/dist/number/enum/int8.d.mts +202 -0
- package/dist/number/enum/int8.d.mts.map +1 -0
- package/dist/number/enum/int8.mjs +296 -0
- package/dist/number/enum/int8.mjs.map +1 -0
- package/dist/number/enum/uint8.d.mts +128 -0
- package/dist/number/enum/uint8.d.mts.map +1 -0
- package/dist/number/enum/uint8.mjs +251 -0
- package/dist/number/enum/uint8.mjs.map +1 -0
- package/dist/number/index.d.mts +5 -0
- package/dist/number/index.d.mts.map +1 -0
- package/dist/number/index.mjs +31 -0
- package/dist/number/index.mjs.map +1 -0
- package/dist/number/num.d.mts +515 -0
- package/dist/number/num.d.mts.map +1 -0
- package/dist/number/num.mjs +513 -0
- package/dist/number/num.mjs.map +1 -0
- package/dist/number/refined-number-utils.d.mts +191 -0
- package/dist/number/refined-number-utils.d.mts.map +1 -0
- package/dist/number/refined-number-utils.mjs +179 -0
- package/dist/number/refined-number-utils.mjs.map +1 -0
- package/dist/object/index.d.mts +2 -0
- package/dist/object/index.d.mts.map +1 -0
- package/dist/object/index.mjs +2 -0
- package/dist/object/index.mjs.map +1 -0
- package/dist/object/object.d.mts +296 -0
- package/dist/object/object.d.mts.map +1 -0
- package/dist/object/object.mjs +295 -0
- package/dist/object/object.mjs.map +1 -0
- package/dist/others/cast-mutable.d.mts +110 -0
- package/dist/others/cast-mutable.d.mts.map +1 -0
- package/dist/others/cast-mutable.mjs +114 -0
- package/dist/others/cast-mutable.mjs.map +1 -0
- package/dist/others/cast-readonly.d.mts +189 -0
- package/dist/others/cast-readonly.d.mts.map +1 -0
- package/dist/others/cast-readonly.mjs +193 -0
- package/dist/others/cast-readonly.mjs.map +1 -0
- package/dist/others/if-then.d.mts +98 -0
- package/dist/others/if-then.d.mts.map +1 -0
- package/dist/others/if-then.mjs +100 -0
- package/dist/others/if-then.mjs.map +1 -0
- package/dist/others/index.d.mts +8 -0
- package/dist/others/index.d.mts.map +1 -0
- package/dist/others/index.mjs +8 -0
- package/dist/others/index.mjs.map +1 -0
- package/dist/others/map-nullable.d.mts +151 -0
- package/dist/others/map-nullable.d.mts.map +1 -0
- package/dist/others/map-nullable.mjs +159 -0
- package/dist/others/map-nullable.mjs.map +1 -0
- package/dist/others/memoize-function.d.mts +173 -0
- package/dist/others/memoize-function.d.mts.map +1 -0
- package/dist/others/memoize-function.mjs +189 -0
- package/dist/others/memoize-function.mjs.map +1 -0
- package/dist/others/tuple.d.mts +159 -0
- package/dist/others/tuple.d.mts.map +1 -0
- package/dist/others/tuple.mjs +161 -0
- package/dist/others/tuple.mjs.map +1 -0
- package/dist/others/unknown-to-string.d.mts +180 -0
- package/dist/others/unknown-to-string.d.mts.map +1 -0
- package/dist/others/unknown-to-string.mjs +211 -0
- package/dist/others/unknown-to-string.mjs.map +1 -0
- package/dist/tsconfig.json +1 -0
- package/package.json +16 -14
|
@@ -0,0 +1,432 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type guard that checks if a value is `undefined`.
|
|
3
|
+
*
|
|
4
|
+
* **Type Narrowing Behavior:**
|
|
5
|
+
* - Narrows the input type to `undefined` when `true`
|
|
6
|
+
* - Useful for explicit undefined checks
|
|
7
|
+
*
|
|
8
|
+
* @param u - The value to check
|
|
9
|
+
* @returns `true` if `u` is `undefined`, `false` otherwise.
|
|
10
|
+
* When `true`, TypeScript narrows the type to `undefined`.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```typescript
|
|
14
|
+
* const value: string | undefined = getValue();
|
|
15
|
+
*
|
|
16
|
+
* if (isUndefined(value)) {
|
|
17
|
+
* // value is now typed as undefined
|
|
18
|
+
* console.log('Value is undefined');
|
|
19
|
+
* } else {
|
|
20
|
+
* // value is now typed as string
|
|
21
|
+
* console.log('Value length:', value.length);
|
|
22
|
+
* }
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
const isUndefined = (u) => u === undefined;
|
|
26
|
+
/**
|
|
27
|
+
* Type guard that checks if a value is not `undefined`.
|
|
28
|
+
*
|
|
29
|
+
* **Type Narrowing Behavior:**
|
|
30
|
+
* - Excludes `undefined` from the input type when `true`
|
|
31
|
+
* - Preserves all other types in the union
|
|
32
|
+
* - Commonly used to filter out undefined values
|
|
33
|
+
*
|
|
34
|
+
* @template T - The type of the input value
|
|
35
|
+
* @param u - The value to check
|
|
36
|
+
* @returns `true` if `u` is not `undefined`, `false` otherwise.
|
|
37
|
+
* When `true`, TypeScript excludes `undefined` from the type.
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* ```typescript
|
|
41
|
+
* const items: (string | undefined)[] = ['a', undefined, 'b', undefined, 'c'];
|
|
42
|
+
*
|
|
43
|
+
* const definedItems = items.filter(isNotUndefined);
|
|
44
|
+
* // definedItems is now string[] - undefined values are filtered out
|
|
45
|
+
*
|
|
46
|
+
* definedItems.forEach(item => {
|
|
47
|
+
* // item is guaranteed to be string, not undefined
|
|
48
|
+
* console.log(item.toUpperCase());
|
|
49
|
+
* });
|
|
50
|
+
* ```
|
|
51
|
+
*/
|
|
52
|
+
const isNotUndefined = (u) => u !== undefined;
|
|
53
|
+
/**
|
|
54
|
+
* Type guard that checks if a value is a boolean.
|
|
55
|
+
*
|
|
56
|
+
* **Type Narrowing Behavior:**
|
|
57
|
+
* - Narrows `unknown` to `boolean` when `true`
|
|
58
|
+
* - Preserves literal boolean types (`true | false`)
|
|
59
|
+
*
|
|
60
|
+
* @param u - The value to check
|
|
61
|
+
* @returns `true` if `u` is a boolean, `false` otherwise.
|
|
62
|
+
* When `true`, TypeScript narrows the type to `boolean`.
|
|
63
|
+
*
|
|
64
|
+
* @example
|
|
65
|
+
* ```typescript
|
|
66
|
+
* const userInput: unknown = parseInput();
|
|
67
|
+
*
|
|
68
|
+
* if (isBoolean(userInput)) {
|
|
69
|
+
* // userInput is now typed as boolean
|
|
70
|
+
* console.log('Boolean value:', userInput ? 'true' : 'false');
|
|
71
|
+
* }
|
|
72
|
+
* ```
|
|
73
|
+
*/
|
|
74
|
+
const isBoolean = (u) => typeof u === 'boolean';
|
|
75
|
+
/**
|
|
76
|
+
* Type guard that checks if a value is not a boolean.
|
|
77
|
+
*
|
|
78
|
+
* **Type Narrowing Behavior:**
|
|
79
|
+
* - Excludes `boolean` from the input type when `true`
|
|
80
|
+
* - Preserves all other types in the union
|
|
81
|
+
*
|
|
82
|
+
* @template T - The type of the input value
|
|
83
|
+
* @param u - The value to check
|
|
84
|
+
* @returns `true` if `u` is not a boolean, `false` otherwise.
|
|
85
|
+
* When `true`, TypeScript excludes `boolean` from the type.
|
|
86
|
+
*
|
|
87
|
+
* @example
|
|
88
|
+
* ```typescript
|
|
89
|
+
* type MixedValue = string | number | boolean;
|
|
90
|
+
* const value: MixedValue = getValue();
|
|
91
|
+
*
|
|
92
|
+
* if (isNotBoolean(value)) {
|
|
93
|
+
* // value is now string | number
|
|
94
|
+
* console.log('Non-boolean value:', value);
|
|
95
|
+
* }
|
|
96
|
+
* ```
|
|
97
|
+
*/
|
|
98
|
+
const isNotBoolean = (u) => typeof u !== 'boolean';
|
|
99
|
+
/**
|
|
100
|
+
* Type guard that checks if a value is a number.
|
|
101
|
+
*
|
|
102
|
+
* **Type Narrowing Behavior:**
|
|
103
|
+
* - Narrows `unknown` to `number` when `true`
|
|
104
|
+
* - Includes `NaN`, `Infinity`, and `-Infinity` as valid numbers
|
|
105
|
+
* - Preserves literal number types when possible
|
|
106
|
+
*
|
|
107
|
+
* @param u - The value to check
|
|
108
|
+
* @returns `true` if `u` is a number, `false` otherwise.
|
|
109
|
+
* When `true`, TypeScript narrows the type to `number`.
|
|
110
|
+
*
|
|
111
|
+
* @example
|
|
112
|
+
* ```typescript
|
|
113
|
+
* const userInput: unknown = parseInput();
|
|
114
|
+
*
|
|
115
|
+
* if (isNumber(userInput)) {
|
|
116
|
+
* // userInput is now typed as number
|
|
117
|
+
* console.log('Number value:', userInput.toFixed(2));
|
|
118
|
+
*
|
|
119
|
+
* // Note: this includes NaN and Infinity
|
|
120
|
+
* if (Number.isFinite(userInput)) {
|
|
121
|
+
* console.log('Finite number:', userInput);
|
|
122
|
+
* }
|
|
123
|
+
* }
|
|
124
|
+
* ```
|
|
125
|
+
*/
|
|
126
|
+
const isNumber = (u) => typeof u === 'number';
|
|
127
|
+
/**
|
|
128
|
+
* Type guard that checks if a value is not a number.
|
|
129
|
+
*
|
|
130
|
+
* **Type Narrowing Behavior:**
|
|
131
|
+
* - Excludes `number` from the input type when `true`
|
|
132
|
+
* - Preserves all other types in the union
|
|
133
|
+
*
|
|
134
|
+
* @template T - The type of the input value
|
|
135
|
+
* @param u - The value to check
|
|
136
|
+
* @returns `true` if `u` is not a number, `false` otherwise.
|
|
137
|
+
* When `true`, TypeScript excludes `number` from the type.
|
|
138
|
+
*
|
|
139
|
+
* @example
|
|
140
|
+
* ```typescript
|
|
141
|
+
* type Value = string | number | boolean;
|
|
142
|
+
* const values: Value[] = ['hello', 42, true, 3.14, false];
|
|
143
|
+
*
|
|
144
|
+
* const nonNumbers = values.filter(isNotNumber);
|
|
145
|
+
* // nonNumbers is now (string | boolean)[] - numbers are filtered out
|
|
146
|
+
* ```
|
|
147
|
+
*/
|
|
148
|
+
const isNotNumber = (u) => typeof u !== 'number';
|
|
149
|
+
/**
|
|
150
|
+
* Type guard that checks if a value is a bigint.
|
|
151
|
+
*
|
|
152
|
+
* **Type Narrowing Behavior:**
|
|
153
|
+
* - Narrows `unknown` to `bigint` when `true`
|
|
154
|
+
* - Identifies values created with `BigInt()` constructor or `n` suffix
|
|
155
|
+
*
|
|
156
|
+
* @param u - The value to check
|
|
157
|
+
* @returns `true` if `u` is a bigint, `false` otherwise.
|
|
158
|
+
* When `true`, TypeScript narrows the type to `bigint`.
|
|
159
|
+
*
|
|
160
|
+
* @example
|
|
161
|
+
* ```typescript
|
|
162
|
+
* const userInput: unknown = parseInput();
|
|
163
|
+
*
|
|
164
|
+
* if (isBigint(userInput)) {
|
|
165
|
+
* // userInput is now typed as bigint
|
|
166
|
+
* console.log('BigInt value:', userInput.toString());
|
|
167
|
+
* const doubled = userInput * 2n; // Safe bigint operations
|
|
168
|
+
* }
|
|
169
|
+
* ```
|
|
170
|
+
*/
|
|
171
|
+
const isBigint = (u) => typeof u === 'bigint';
|
|
172
|
+
/**
|
|
173
|
+
* Type guard that checks if a value is not a bigint.
|
|
174
|
+
*
|
|
175
|
+
* **Type Narrowing Behavior:**
|
|
176
|
+
* - Excludes `bigint` from the input type when `true`
|
|
177
|
+
* - Preserves all other types in the union
|
|
178
|
+
*
|
|
179
|
+
* @template T - The type of the input value
|
|
180
|
+
* @param u - The value to check
|
|
181
|
+
* @returns `true` if `u` is not a bigint, `false` otherwise.
|
|
182
|
+
* When `true`, TypeScript excludes `bigint` from the type.
|
|
183
|
+
*
|
|
184
|
+
* @example
|
|
185
|
+
* ```typescript
|
|
186
|
+
* type NumericValue = number | bigint;
|
|
187
|
+
* const value: NumericValue = getValue();
|
|
188
|
+
*
|
|
189
|
+
* if (isNotBigint(value)) {
|
|
190
|
+
* // value is now number
|
|
191
|
+
* console.log('Regular number:', value.toFixed(2));
|
|
192
|
+
* }
|
|
193
|
+
* ```
|
|
194
|
+
*/
|
|
195
|
+
const isNotBigint = (u) => typeof u !== 'bigint';
|
|
196
|
+
/**
|
|
197
|
+
* Type guard that checks if a value is a string.
|
|
198
|
+
*
|
|
199
|
+
* **Type Narrowing Behavior:**
|
|
200
|
+
* - Narrows `unknown` to `string` when `true`
|
|
201
|
+
* - Preserves literal string types when possible
|
|
202
|
+
* - Includes empty strings
|
|
203
|
+
*
|
|
204
|
+
* @param u - The value to check
|
|
205
|
+
* @returns `true` if `u` is a string, `false` otherwise.
|
|
206
|
+
* When `true`, TypeScript narrows the type to `string`.
|
|
207
|
+
*
|
|
208
|
+
* @example
|
|
209
|
+
* ```typescript
|
|
210
|
+
* const userInput: unknown = parseInput();
|
|
211
|
+
*
|
|
212
|
+
* if (isString(userInput)) {
|
|
213
|
+
* // userInput is now typed as string
|
|
214
|
+
* console.log('String length:', userInput.length);
|
|
215
|
+
* console.log('Uppercase:', userInput.toUpperCase());
|
|
216
|
+
*
|
|
217
|
+
* // You can further check for non-empty strings
|
|
218
|
+
* if (userInput.length > 0) {
|
|
219
|
+
* console.log('Non-empty string:', userInput);
|
|
220
|
+
* }
|
|
221
|
+
* }
|
|
222
|
+
* ```
|
|
223
|
+
*/
|
|
224
|
+
const isString = (u) => typeof u === 'string';
|
|
225
|
+
/**
|
|
226
|
+
* Type guard that checks if a value is not a string.
|
|
227
|
+
*
|
|
228
|
+
* **Type Narrowing Behavior:**
|
|
229
|
+
* - Excludes `string` from the input type when `true`
|
|
230
|
+
* - Preserves all other types in the union
|
|
231
|
+
*
|
|
232
|
+
* @template T - The type of the input value
|
|
233
|
+
* @param u - The value to check
|
|
234
|
+
* @returns `true` if `u` is not a string, `false` otherwise.
|
|
235
|
+
* When `true`, TypeScript excludes `string` from the type.
|
|
236
|
+
*
|
|
237
|
+
* @example
|
|
238
|
+
* ```typescript
|
|
239
|
+
* type Value = string | number | boolean;
|
|
240
|
+
* const mixedValues: Value[] = ['hello', 42, true, 'world', 3.14];
|
|
241
|
+
*
|
|
242
|
+
* const nonStrings = mixedValues.filter(isNotString);
|
|
243
|
+
* // nonStrings is now (number | boolean)[] - strings are filtered out
|
|
244
|
+
* ```
|
|
245
|
+
*/
|
|
246
|
+
const isNotString = (u) => typeof u !== 'string';
|
|
247
|
+
/**
|
|
248
|
+
* Type guard that checks if a value is a symbol.
|
|
249
|
+
*
|
|
250
|
+
* **Type Narrowing Behavior:**
|
|
251
|
+
* - Narrows `unknown` to `symbol` when `true`
|
|
252
|
+
* - Identifies values created with `Symbol()` constructor
|
|
253
|
+
*
|
|
254
|
+
* @param u - The value to check
|
|
255
|
+
* @returns `true` if `u` is a symbol, `false` otherwise.
|
|
256
|
+
* When `true`, TypeScript narrows the type to `symbol`.
|
|
257
|
+
*
|
|
258
|
+
* @example
|
|
259
|
+
* ```typescript
|
|
260
|
+
* const userInput: unknown = parseInput();
|
|
261
|
+
*
|
|
262
|
+
* if (isSymbol(userInput)) {
|
|
263
|
+
* // userInput is now typed as symbol
|
|
264
|
+
* console.log('Symbol description:', userInput.description);
|
|
265
|
+
* console.log('Symbol string:', userInput.toString());
|
|
266
|
+
* }
|
|
267
|
+
* ```
|
|
268
|
+
*/
|
|
269
|
+
const isSymbol = (u) => typeof u === 'symbol';
|
|
270
|
+
/**
|
|
271
|
+
* Type guard that checks if a value is not a symbol.
|
|
272
|
+
*
|
|
273
|
+
* **Type Narrowing Behavior:**
|
|
274
|
+
* - Excludes `symbol` from the input type when `true`
|
|
275
|
+
* - Preserves all other types in the union
|
|
276
|
+
*
|
|
277
|
+
* @template T - The type of the input value
|
|
278
|
+
* @param u - The value to check
|
|
279
|
+
* @returns `true` if `u` is not a symbol, `false` otherwise.
|
|
280
|
+
* When `true`, TypeScript excludes `symbol` from the type.
|
|
281
|
+
*
|
|
282
|
+
* @example
|
|
283
|
+
* ```typescript
|
|
284
|
+
* type PropertyKey = string | number | symbol;
|
|
285
|
+
* const key: PropertyKey = getPropertyKey();
|
|
286
|
+
*
|
|
287
|
+
* if (isNotSymbol(key)) {
|
|
288
|
+
* // key is now string | number
|
|
289
|
+
* console.log('Non-symbol key:', key);
|
|
290
|
+
* }
|
|
291
|
+
* ```
|
|
292
|
+
*/
|
|
293
|
+
const isNotSymbol = (u) => typeof u !== 'symbol';
|
|
294
|
+
/**
|
|
295
|
+
* Type guard that checks if a value is `null`.
|
|
296
|
+
*
|
|
297
|
+
* **Type Narrowing Behavior:**
|
|
298
|
+
* - Narrows the input type to `null` when `true`
|
|
299
|
+
* - Useful for explicit null checks
|
|
300
|
+
*
|
|
301
|
+
* @param u - The value to check
|
|
302
|
+
* @returns `true` if `u` is `null`, `false` otherwise.
|
|
303
|
+
* When `true`, TypeScript narrows the type to `null`.
|
|
304
|
+
*
|
|
305
|
+
* @example
|
|
306
|
+
* ```typescript
|
|
307
|
+
* const value: string | null = getValue();
|
|
308
|
+
*
|
|
309
|
+
* if (isNull(value)) {
|
|
310
|
+
* // value is now typed as null
|
|
311
|
+
* console.log('Value is null');
|
|
312
|
+
* } else {
|
|
313
|
+
* // value is now typed as string
|
|
314
|
+
* console.log('Value length:', value.length);
|
|
315
|
+
* }
|
|
316
|
+
* ```
|
|
317
|
+
*/
|
|
318
|
+
const isNull = (u) => u === null;
|
|
319
|
+
/**
|
|
320
|
+
* Type guard that checks if a value is not `null`.
|
|
321
|
+
*
|
|
322
|
+
* **Type Narrowing Behavior:**
|
|
323
|
+
* - Excludes `null` from the input type when `true`
|
|
324
|
+
* - Preserves all other types including `undefined`
|
|
325
|
+
* - Commonly used to filter out null values
|
|
326
|
+
*
|
|
327
|
+
* @template T - The type of the input value (which could be `null`)
|
|
328
|
+
* @param u - The value to check
|
|
329
|
+
* @returns `true` if `u` is not `null`, `false` otherwise.
|
|
330
|
+
* When `true`, TypeScript excludes `null` from the type.
|
|
331
|
+
*
|
|
332
|
+
* @example
|
|
333
|
+
* ```typescript
|
|
334
|
+
* const items: (string | null)[] = ['a', null, 'b', null, 'c'];
|
|
335
|
+
*
|
|
336
|
+
* const nonNullItems = items.filter(isNotNull);
|
|
337
|
+
* // nonNullItems is now string[] - null values are filtered out
|
|
338
|
+
*
|
|
339
|
+
* nonNullItems.forEach(item => {
|
|
340
|
+
* // item is guaranteed to be string, not null
|
|
341
|
+
* console.log(item.toUpperCase());
|
|
342
|
+
* });
|
|
343
|
+
* ```
|
|
344
|
+
*/
|
|
345
|
+
const isNotNull = (u) => u !== null;
|
|
346
|
+
/**
|
|
347
|
+
* Type guard that checks if a value is `null` or `undefined` (nullish).
|
|
348
|
+
*
|
|
349
|
+
* This function uses the loose equality operator (`==`) to check for both `null` and `undefined`
|
|
350
|
+
* in a single comparison, which is the standard JavaScript idiom for nullish checks.
|
|
351
|
+
*
|
|
352
|
+
* **Type Narrowing Behavior:**
|
|
353
|
+
* - Narrows the input type to `null | undefined` when `true`
|
|
354
|
+
* - Useful for checking if a value is "nullish" (either null or undefined)
|
|
355
|
+
*
|
|
356
|
+
* @param u - The value to check
|
|
357
|
+
* @returns `true` if `u` is `null` or `undefined`, `false` otherwise.
|
|
358
|
+
* When `true`, TypeScript narrows the type to `null | undefined`.
|
|
359
|
+
*
|
|
360
|
+
* @example
|
|
361
|
+
* ```typescript
|
|
362
|
+
* const value: string | null | undefined = getValue();
|
|
363
|
+
*
|
|
364
|
+
* if (isNullish(value)) {
|
|
365
|
+
* // value is now typed as null | undefined
|
|
366
|
+
* console.log('Value is nullish');
|
|
367
|
+
* } else {
|
|
368
|
+
* // value is now typed as string
|
|
369
|
+
* console.log('Value length:', value.length);
|
|
370
|
+
* }
|
|
371
|
+
* ```
|
|
372
|
+
*/
|
|
373
|
+
const isNullish = (u) => u == null;
|
|
374
|
+
/**
|
|
375
|
+
* Type guard that checks if a value is not `null` or `undefined` (non-nullish).
|
|
376
|
+
*
|
|
377
|
+
* This function uses the loose inequality operator (`!=`) to check that a value is neither
|
|
378
|
+
* `null` nor `undefined` in a single comparison. This is equivalent to TypeScript's
|
|
379
|
+
* `NonNullable<T>` utility type.
|
|
380
|
+
*
|
|
381
|
+
* **Type Narrowing Behavior:**
|
|
382
|
+
* - Excludes both `null` and `undefined` from the input type when `true`
|
|
383
|
+
* - Equivalent to applying TypeScript's `NonNullable<T>` utility type
|
|
384
|
+
* - Commonly used to filter out nullish values from arrays
|
|
385
|
+
*
|
|
386
|
+
* @template T - The type of the input value
|
|
387
|
+
* @param u - The value to check
|
|
388
|
+
* @returns `true` if `u` is not `null` and not `undefined`, `false` otherwise.
|
|
389
|
+
* When `true`, TypeScript narrows the type to `NonNullable<T>`.
|
|
390
|
+
*
|
|
391
|
+
* @example
|
|
392
|
+
* ```typescript
|
|
393
|
+
* const items: (string | null | undefined)[] = [
|
|
394
|
+
* 'hello',
|
|
395
|
+
* null,
|
|
396
|
+
* 'world',
|
|
397
|
+
* undefined,
|
|
398
|
+
* 'test'
|
|
399
|
+
* ];
|
|
400
|
+
*
|
|
401
|
+
* const definedItems = items.filter(isNonNullish);
|
|
402
|
+
* // definedItems is now string[] - both null and undefined values are filtered out
|
|
403
|
+
*
|
|
404
|
+
* definedItems.forEach(item => {
|
|
405
|
+
* // item is guaranteed to be string, never null or undefined
|
|
406
|
+
* console.log(item.toUpperCase());
|
|
407
|
+
* });
|
|
408
|
+
* ```
|
|
409
|
+
*
|
|
410
|
+
* @example
|
|
411
|
+
* Progressive validation with optional chaining alternative:
|
|
412
|
+
* ```typescript
|
|
413
|
+
* interface User {
|
|
414
|
+
* profile?: {
|
|
415
|
+
* name?: string;
|
|
416
|
+
* email?: string;
|
|
417
|
+
* };
|
|
418
|
+
* }
|
|
419
|
+
*
|
|
420
|
+
* const user: User = getUser();
|
|
421
|
+
*
|
|
422
|
+
* // Instead of optional chaining: user.profile?.name
|
|
423
|
+
* if (isNonNullish(user.profile) && isNonNullish(user.profile.name)) {
|
|
424
|
+
* // user.profile.name is now guaranteed to be string
|
|
425
|
+
* console.log('User name:', user.profile.name.toUpperCase());
|
|
426
|
+
* }
|
|
427
|
+
* ```
|
|
428
|
+
*/
|
|
429
|
+
const isNonNullish = (u) => u != null;
|
|
430
|
+
|
|
431
|
+
export { isBigint, isBoolean, isNonNullish, isNotBigint, isNotBoolean, isNotNull, isNotNumber, isNotString, isNotSymbol, isNotUndefined, isNull, isNullish, isNumber, isString, isSymbol, isUndefined };
|
|
432
|
+
//# sourceMappingURL=is-type.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"is-type.mjs","sources":["../../src/guard/is-type.mts"],"sourcesContent":[null],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;AAuBG;AACI,MAAM,WAAW,GAAG,CAAC,CAAU,KAAqB,CAAC,KAAK;AAEjE;;;;;;;;;;;;;;;;;;;;;;;;;AAyBG;AACI,MAAM,cAAc,GAAG,CAAK,CAAI,KACrC,CAAC,KAAK;AAER;;;;;;;;;;;;;;;;;;;;AAoBG;AACI,MAAM,SAAS,GAAG,CAAC,CAAU,KAAmB,OAAO,CAAC,KAAK;AAEpE;;;;;;;;;;;;;;;;;;;;;;AAsBG;AACI,MAAM,YAAY,GAAG,CAAK,CAAI,KACnC,OAAO,CAAC,KAAK;AAEf;;;;;;;;;;;;;;;;;;;;;;;;;;AA0BG;AACI,MAAM,QAAQ,GAAG,CAAC,CAAU,KAAkB,OAAO,CAAC,KAAK;AAElE;;;;;;;;;;;;;;;;;;;;AAoBG;AACI,MAAM,WAAW,GAAG,CAAK,CAAI,KAClC,OAAO,CAAC,KAAK;AAEf;;;;;;;;;;;;;;;;;;;;;AAqBG;AACI,MAAM,QAAQ,GAAG,CAAC,CAAU,KAAkB,OAAO,CAAC,KAAK;AAElE;;;;;;;;;;;;;;;;;;;;;;AAsBG;AACI,MAAM,WAAW,GAAG,CAAK,CAAI,KAClC,OAAO,CAAC,KAAK;AAEf;;;;;;;;;;;;;;;;;;;;;;;;;;;AA2BG;AACI,MAAM,QAAQ,GAAG,CAAC,CAAU,KAAkB,OAAO,CAAC,KAAK;AAElE;;;;;;;;;;;;;;;;;;;;AAoBG;AACI,MAAM,WAAW,GAAG,CAAK,CAAI,KAClC,OAAO,CAAC,KAAK;AAEf;;;;;;;;;;;;;;;;;;;;;AAqBG;AACI,MAAM,QAAQ,GAAG,CAAC,CAAU,KAAkB,OAAO,CAAC,KAAK;AAElE;;;;;;;;;;;;;;;;;;;;;;AAsBG;AACI,MAAM,WAAW,GAAG,CAAK,CAAI,KAClC,OAAO,CAAC,KAAK;AAEf;;;;;;;;;;;;;;;;;;;;;;;AAuBG;AACI,MAAM,MAAM,GAAG,CAAC,CAAU,KAAgB,CAAC,KAAK;AAEvD;;;;;;;;;;;;;;;;;;;;;;;;;AAyBG;AACI,MAAM,SAAS,GAAG,CAAK,CAAW,KAAa,CAAC,KAAK;AAE5D;;;;;;;;;;;;;;;;;;;;;;;;;;AA0BG;AACI,MAAM,SAAS,GAAG,CAAC,CAAU,KAA4B,CAAC,IAAI;AAErE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAsDG;AACI,MAAM,YAAY,GAAG,CAAK,CAAI,KAA0B,CAAC,IAAI;;;;"}
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type guard that checks if a key exists as an own property in an object.
|
|
3
|
+
*
|
|
4
|
+
* This function is similar to `hasKey()` but with reversed parameter order and different
|
|
5
|
+
* type narrowing behavior. While `hasKey()` narrows the object type, `keyIsIn()` narrows
|
|
6
|
+
* the key type to be a valid key of the given object.
|
|
7
|
+
*
|
|
8
|
+
* **Type Narrowing Behavior:**
|
|
9
|
+
* - Narrows the key type to be a key that exists in the object (`K & keyof R`)
|
|
10
|
+
* - Useful when you have a dynamic key and want to ensure it's valid for a specific object
|
|
11
|
+
* - The object type remains unchanged
|
|
12
|
+
*
|
|
13
|
+
* **Implementation:** Uses `Object.hasOwn()` to check for own properties (not inherited).
|
|
14
|
+
*
|
|
15
|
+
* @template K - The type of the key to check, must extend PropertyKey (string | number | symbol)
|
|
16
|
+
* @template R - The type of the record (object), must extend UnknownRecord
|
|
17
|
+
* @param key - The key to check for
|
|
18
|
+
* @param obj - The object to check within
|
|
19
|
+
* @returns `true` if `key` is an own property of `obj`, `false` otherwise.
|
|
20
|
+
* When `true`, TypeScript narrows the key type to be a valid key of the object.
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* Basic usage with known object structure:
|
|
24
|
+
* ```typescript
|
|
25
|
+
* const obj = { a: 1, b: 2, c: 3 };
|
|
26
|
+
* const userInput: string = getUserInput(); // Could be any string
|
|
27
|
+
*
|
|
28
|
+
* if (keyIsIn(userInput, obj)) {
|
|
29
|
+
* // userInput is now narrowed to 'a' | 'b' | 'c'
|
|
30
|
+
* const value = obj[userInput]; // Type-safe access, value is number
|
|
31
|
+
* console.log(`Value for ${userInput}:`, value);
|
|
32
|
+
* } else {
|
|
33
|
+
* console.log(`Key '${userInput}' not found in object`);
|
|
34
|
+
* }
|
|
35
|
+
* ```
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* Dynamic key validation for safe property access:
|
|
39
|
+
* ```typescript
|
|
40
|
+
* const config = {
|
|
41
|
+
* apiUrl: 'https://api.example.com',
|
|
42
|
+
* timeout: 5000,
|
|
43
|
+
* retries: 3
|
|
44
|
+
* } as const;
|
|
45
|
+
*
|
|
46
|
+
* function getConfigValue(key: string): unknown {
|
|
47
|
+
* if (keyIsIn(key, config)) {
|
|
48
|
+
* // key is now narrowed to 'apiUrl' | 'timeout' | 'retries'
|
|
49
|
+
* return config[key]; // Safe access with proper typing
|
|
50
|
+
* }
|
|
51
|
+
*
|
|
52
|
+
* throw new Error(`Invalid config key: ${key}`);
|
|
53
|
+
* }
|
|
54
|
+
*
|
|
55
|
+
* // Usage
|
|
56
|
+
* const apiUrl = getConfigValue('apiUrl'); // Returns string
|
|
57
|
+
* const timeout = getConfigValue('timeout'); // Returns number
|
|
58
|
+
* // getConfigValue('invalid') would throw an error
|
|
59
|
+
* ```
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* Form field validation:
|
|
63
|
+
* ```typescript
|
|
64
|
+
* interface FormData {
|
|
65
|
+
* name: string;
|
|
66
|
+
* email: string;
|
|
67
|
+
* age: number;
|
|
68
|
+
* }
|
|
69
|
+
*
|
|
70
|
+
* const formData: FormData = getFormData();
|
|
71
|
+
* const requiredFields: readonly string[] = ['name', 'email'] as const;
|
|
72
|
+
*
|
|
73
|
+
* function validateRequiredFields(data: FormData): string[] {
|
|
74
|
+
* const errors: string[] = [];
|
|
75
|
+
*
|
|
76
|
+
* for (const field of requiredFields) {
|
|
77
|
+
* if (keyIsIn(field, data)) {
|
|
78
|
+
* // field is now narrowed to keyof FormData
|
|
79
|
+
* const value = data[field];
|
|
80
|
+
*
|
|
81
|
+
* if (typeof value === 'string' && value.trim() === '') {
|
|
82
|
+
* errors.push(`${field} is required`);
|
|
83
|
+
* }
|
|
84
|
+
* }
|
|
85
|
+
* }
|
|
86
|
+
*
|
|
87
|
+
* return errors;
|
|
88
|
+
* }
|
|
89
|
+
* ```
|
|
90
|
+
*
|
|
91
|
+
* @example
|
|
92
|
+
* Safe object property iteration:
|
|
93
|
+
* ```typescript
|
|
94
|
+
* const userPreferences = {
|
|
95
|
+
* theme: 'dark',
|
|
96
|
+
* language: 'en',
|
|
97
|
+
* notifications: true
|
|
98
|
+
* };
|
|
99
|
+
*
|
|
100
|
+
* const settingsToUpdate: string[] = getSettingsFromUser();
|
|
101
|
+
*
|
|
102
|
+
* function updatePreferences(updates: Record<string, unknown>) {
|
|
103
|
+
* const validUpdates: Partial<typeof userPreferences> = {};
|
|
104
|
+
*
|
|
105
|
+
* for (const [key, value] of Object.entries(updates)) {
|
|
106
|
+
* if (keyIsIn(key, userPreferences)) {
|
|
107
|
+
* // key is now narrowed to valid preference keys
|
|
108
|
+
* validUpdates[key] = value as typeof userPreferences[typeof key];
|
|
109
|
+
* } else {
|
|
110
|
+
* console.warn(`Unknown preference key: ${key}`);
|
|
111
|
+
* }
|
|
112
|
+
* }
|
|
113
|
+
*
|
|
114
|
+
* return { ...userPreferences, ...validUpdates };
|
|
115
|
+
* }
|
|
116
|
+
* ```
|
|
117
|
+
*
|
|
118
|
+
* @example
|
|
119
|
+
* Comparison with hasKey() - different narrowing behavior:
|
|
120
|
+
* ```typescript
|
|
121
|
+
* const obj = { x: 10, y: 20 };
|
|
122
|
+
* const key: string = 'x';
|
|
123
|
+
*
|
|
124
|
+
* // Using keyIsIn - narrows the key type
|
|
125
|
+
* if (keyIsIn(key, obj)) {
|
|
126
|
+
* // key is now 'x' | 'y'
|
|
127
|
+
* const value = obj[key]; // Safe access
|
|
128
|
+
* }
|
|
129
|
+
*
|
|
130
|
+
* // Using hasKey - narrows the object type
|
|
131
|
+
* if (hasKey(obj, key)) {
|
|
132
|
+
* // obj type is narrowed to guarantee the key exists
|
|
133
|
+
* const value = obj.x; // Direct access
|
|
134
|
+
* }
|
|
135
|
+
* ```
|
|
136
|
+
*
|
|
137
|
+
* @example
|
|
138
|
+
* Working with union types:
|
|
139
|
+
* ```typescript
|
|
140
|
+
* type Config =
|
|
141
|
+
* | { type: 'database'; host: string; port: number }
|
|
142
|
+
* | { type: 'file'; path: string }
|
|
143
|
+
* | { type: 'memory'; maxSize: number };
|
|
144
|
+
*
|
|
145
|
+
* function getConfigProperty(config: Config, propName: string): unknown {
|
|
146
|
+
* if (keyIsIn(propName, config)) {
|
|
147
|
+
* // propName is narrowed to valid keys for the specific config type
|
|
148
|
+
* return config[propName];
|
|
149
|
+
* }
|
|
150
|
+
*
|
|
151
|
+
* return undefined;
|
|
152
|
+
* }
|
|
153
|
+
* ```
|
|
154
|
+
*
|
|
155
|
+
* @see {@link hasKey} - Similar function that narrows the object type instead of the key type
|
|
156
|
+
*/
|
|
157
|
+
export declare const keyIsIn: <const K extends PropertyKey, const R extends UnknownRecord>(key: K, obj: R) => key is K & keyof typeof obj;
|
|
158
|
+
//# sourceMappingURL=key-is-in.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"key-is-in.d.mts","sourceRoot":"","sources":["../../src/guard/key-is-in.mts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2JG;AACH,eAAO,MAAM,OAAO,GAClB,KAAK,CAAC,CAAC,SAAS,WAAW,EAC3B,KAAK,CAAC,CAAC,SAAS,aAAa,EAE7B,KAAK,CAAC,EACN,KAAK,CAAC,KACL,GAAG,IAAI,CAAC,GAAG,MAAM,OAAO,GAA8B,CAAC"}
|