typetify 0.1.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/LICENSE +21 -0
- package/README.md +327 -0
- package/dist/async/index.d.mts +152 -0
- package/dist/async/index.d.ts +152 -0
- package/dist/async/index.js +49 -0
- package/dist/async/index.js.map +1 -0
- package/dist/async/index.mjs +4 -0
- package/dist/async/index.mjs.map +1 -0
- package/dist/chunk-2LJ6NZ6K.js +108 -0
- package/dist/chunk-2LJ6NZ6K.js.map +1 -0
- package/dist/chunk-44Y5JSGU.js +145 -0
- package/dist/chunk-44Y5JSGU.js.map +1 -0
- package/dist/chunk-4NXETABV.mjs +116 -0
- package/dist/chunk-4NXETABV.mjs.map +1 -0
- package/dist/chunk-6ZBTL74K.js +129 -0
- package/dist/chunk-6ZBTL74K.js.map +1 -0
- package/dist/chunk-CN3GYRJN.mjs +137 -0
- package/dist/chunk-CN3GYRJN.mjs.map +1 -0
- package/dist/chunk-CNTE6ZVH.js +359 -0
- package/dist/chunk-CNTE6ZVH.js.map +1 -0
- package/dist/chunk-DBENOSTA.js +156 -0
- package/dist/chunk-DBENOSTA.js.map +1 -0
- package/dist/chunk-DWIG5GF2.js +135 -0
- package/dist/chunk-DWIG5GF2.js.map +1 -0
- package/dist/chunk-EAUTTWTQ.mjs +231 -0
- package/dist/chunk-EAUTTWTQ.mjs.map +1 -0
- package/dist/chunk-FXWYPHA3.mjs +13 -0
- package/dist/chunk-FXWYPHA3.mjs.map +1 -0
- package/dist/chunk-GS3PP67B.js +200 -0
- package/dist/chunk-GS3PP67B.js.map +1 -0
- package/dist/chunk-J5LGTIGS.mjs +9 -0
- package/dist/chunk-J5LGTIGS.mjs.map +1 -0
- package/dist/chunk-JAOGY4JO.mjs +1007 -0
- package/dist/chunk-JAOGY4JO.mjs.map +1 -0
- package/dist/chunk-JB6UXRKD.mjs +97 -0
- package/dist/chunk-JB6UXRKD.mjs.map +1 -0
- package/dist/chunk-JQHUBZ4M.js +88 -0
- package/dist/chunk-JQHUBZ4M.js.map +1 -0
- package/dist/chunk-L3M7LGKL.mjs +128 -0
- package/dist/chunk-L3M7LGKL.mjs.map +1 -0
- package/dist/chunk-LT7JK7RJ.js +87 -0
- package/dist/chunk-LT7JK7RJ.js.map +1 -0
- package/dist/chunk-OEJK37LO.mjs +328 -0
- package/dist/chunk-OEJK37LO.mjs.map +1 -0
- package/dist/chunk-OPVES6W2.js +16 -0
- package/dist/chunk-OPVES6W2.js.map +1 -0
- package/dist/chunk-OWNUKWXV.js +291 -0
- package/dist/chunk-OWNUKWXV.js.map +1 -0
- package/dist/chunk-PQTXSQ4P.js +369 -0
- package/dist/chunk-PQTXSQ4P.js.map +1 -0
- package/dist/chunk-PZ5AY32C.js +11 -0
- package/dist/chunk-PZ5AY32C.js.map +1 -0
- package/dist/chunk-QFR7DVAJ.mjs +63 -0
- package/dist/chunk-QFR7DVAJ.mjs.map +1 -0
- package/dist/chunk-SGQNLTRK.js +73 -0
- package/dist/chunk-SGQNLTRK.js.map +1 -0
- package/dist/chunk-SIA5BSVY.js +1054 -0
- package/dist/chunk-SIA5BSVY.js.map +1 -0
- package/dist/chunk-SRDWUHDY.mjs +188 -0
- package/dist/chunk-SRDWUHDY.mjs.map +1 -0
- package/dist/chunk-TXU7NTT4.js +249 -0
- package/dist/chunk-TXU7NTT4.js.map +1 -0
- package/dist/chunk-TZEWREAC.mjs +277 -0
- package/dist/chunk-TZEWREAC.mjs.map +1 -0
- package/dist/chunk-V6CWFDIJ.mjs +123 -0
- package/dist/chunk-V6CWFDIJ.mjs.map +1 -0
- package/dist/chunk-YBJC5WMX.mjs +341 -0
- package/dist/chunk-YBJC5WMX.mjs.map +1 -0
- package/dist/chunk-YOPAXITF.mjs +75 -0
- package/dist/chunk-YOPAXITF.mjs.map +1 -0
- package/dist/chunk-ZE4FDBRI.mjs +79 -0
- package/dist/chunk-ZE4FDBRI.mjs.map +1 -0
- package/dist/collection/index.d.mts +291 -0
- package/dist/collection/index.d.ts +291 -0
- package/dist/collection/index.js +125 -0
- package/dist/collection/index.js.map +1 -0
- package/dist/collection/index.mjs +4 -0
- package/dist/collection/index.mjs.map +1 -0
- package/dist/core/index.d.mts +85 -0
- package/dist/core/index.d.ts +85 -0
- package/dist/core/index.js +41 -0
- package/dist/core/index.js.map +1 -0
- package/dist/core/index.mjs +4 -0
- package/dist/core/index.mjs.map +1 -0
- package/dist/decorator/index.d.mts +165 -0
- package/dist/decorator/index.d.ts +165 -0
- package/dist/decorator/index.js +57 -0
- package/dist/decorator/index.js.map +1 -0
- package/dist/decorator/index.mjs +4 -0
- package/dist/decorator/index.mjs.map +1 -0
- package/dist/dx/index.d.mts +125 -0
- package/dist/dx/index.d.ts +125 -0
- package/dist/dx/index.js +53 -0
- package/dist/dx/index.js.map +1 -0
- package/dist/dx/index.mjs +4 -0
- package/dist/dx/index.mjs.map +1 -0
- package/dist/flow/index.d.mts +165 -0
- package/dist/flow/index.d.ts +165 -0
- package/dist/flow/index.js +50 -0
- package/dist/flow/index.js.map +1 -0
- package/dist/flow/index.mjs +5 -0
- package/dist/flow/index.mjs.map +1 -0
- package/dist/fn/index.d.mts +77 -0
- package/dist/fn/index.d.ts +77 -0
- package/dist/fn/index.js +37 -0
- package/dist/fn/index.js.map +1 -0
- package/dist/fn/index.mjs +4 -0
- package/dist/fn/index.mjs.map +1 -0
- package/dist/guards/index.d.mts +165 -0
- package/dist/guards/index.d.ts +165 -0
- package/dist/guards/index.js +69 -0
- package/dist/guards/index.js.map +1 -0
- package/dist/guards/index.mjs +4 -0
- package/dist/guards/index.mjs.map +1 -0
- package/dist/index.d.mts +228 -0
- package/dist/index.d.ts +228 -0
- package/dist/index.js +775 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +51 -0
- package/dist/index.mjs.map +1 -0
- package/dist/input/index.d.mts +185 -0
- package/dist/input/index.d.ts +185 -0
- package/dist/input/index.js +74 -0
- package/dist/input/index.js.map +1 -0
- package/dist/input/index.mjs +5 -0
- package/dist/input/index.mjs.map +1 -0
- package/dist/iterator/index.d.mts +209 -0
- package/dist/iterator/index.d.ts +209 -0
- package/dist/iterator/index.js +522 -0
- package/dist/iterator/index.js.map +1 -0
- package/dist/iterator/index.mjs +502 -0
- package/dist/iterator/index.mjs.map +1 -0
- package/dist/logic/index.d.mts +531 -0
- package/dist/logic/index.d.ts +531 -0
- package/dist/logic/index.js +416 -0
- package/dist/logic/index.js.map +1 -0
- package/dist/logic/index.mjs +367 -0
- package/dist/logic/index.mjs.map +1 -0
- package/dist/math/index.d.mts +86 -0
- package/dist/math/index.d.ts +86 -0
- package/dist/math/index.js +45 -0
- package/dist/math/index.js.map +1 -0
- package/dist/math/index.mjs +4 -0
- package/dist/math/index.mjs.map +1 -0
- package/dist/narrowing/index.d.mts +429 -0
- package/dist/narrowing/index.d.ts +429 -0
- package/dist/narrowing/index.js +220 -0
- package/dist/narrowing/index.js.map +1 -0
- package/dist/narrowing/index.mjs +186 -0
- package/dist/narrowing/index.mjs.map +1 -0
- package/dist/object/index.d.mts +327 -0
- package/dist/object/index.d.ts +327 -0
- package/dist/object/index.js +113 -0
- package/dist/object/index.js.map +1 -0
- package/dist/object/index.mjs +4 -0
- package/dist/object/index.mjs.map +1 -0
- package/dist/result/index.d.mts +201 -0
- package/dist/result/index.d.ts +201 -0
- package/dist/result/index.js +86 -0
- package/dist/result/index.js.map +1 -0
- package/dist/result/index.mjs +5 -0
- package/dist/result/index.mjs.map +1 -0
- package/dist/schema/index.d.mts +216 -0
- package/dist/schema/index.d.ts +216 -0
- package/dist/schema/index.js +410 -0
- package/dist/schema/index.js.map +1 -0
- package/dist/schema/index.mjs +384 -0
- package/dist/schema/index.mjs.map +1 -0
- package/dist/string/index.d.mts +102 -0
- package/dist/string/index.d.ts +102 -0
- package/dist/string/index.js +49 -0
- package/dist/string/index.js.map +1 -0
- package/dist/string/index.mjs +4 -0
- package/dist/string/index.mjs.map +1 -0
- package/dist/typed/index.d.mts +1962 -0
- package/dist/typed/index.d.ts +1962 -0
- package/dist/typed/index.js +193 -0
- package/dist/typed/index.js.map +1 -0
- package/dist/typed/index.mjs +4 -0
- package/dist/typed/index.mjs.map +1 -0
- package/dist/types-Db0vauC3.d.mts +258 -0
- package/dist/types-Db0vauC3.d.ts +258 -0
- package/dist/types-VsDp2t8s.d.mts +30 -0
- package/dist/types-VsDp2t8s.d.ts +30 -0
- package/package.json +157 -0
|
@@ -0,0 +1,429 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Core types for type narrowing utilities
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* A type guard function that narrows T to S
|
|
6
|
+
*/
|
|
7
|
+
type TypeGuard<T, S extends T> = (value: T) => value is S;
|
|
8
|
+
/**
|
|
9
|
+
* A refinement function that narrows T to S with additional context
|
|
10
|
+
*/
|
|
11
|
+
type Refinement<T, S extends T> = (value: T, index: number, array: readonly T[]) => value is S;
|
|
12
|
+
/**
|
|
13
|
+
* Extracts the narrowed type from a type guard
|
|
14
|
+
*/
|
|
15
|
+
type GuardedType<G> = G extends TypeGuard<unknown, infer S> ? S : never;
|
|
16
|
+
/**
|
|
17
|
+
* Falsy values in JavaScript
|
|
18
|
+
*/
|
|
19
|
+
type Falsy = false | 0 | 0n | '' | null | undefined;
|
|
20
|
+
/**
|
|
21
|
+
* Removes falsy types from a union
|
|
22
|
+
*/
|
|
23
|
+
type Truthy<T> = Exclude<T, Falsy>;
|
|
24
|
+
/**
|
|
25
|
+
* NonNullable but preserves the original type structure
|
|
26
|
+
*/
|
|
27
|
+
type Defined<T> = T extends null | undefined ? never : T;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Filters an array removing null and undefined values with proper type narrowing.
|
|
31
|
+
*
|
|
32
|
+
* This solves the pain point where `array.filter(x => x != null)` returns
|
|
33
|
+
* `(T | null | undefined)[]` instead of `T[]`.
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* const users: (User | null | undefined)[] = [user1, null, user2, undefined]
|
|
37
|
+
* const defined = filterDefined(users)
|
|
38
|
+
* // Type: User[] (not (User | null | undefined)[])
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* // Works with complex union types
|
|
42
|
+
* const mixed: (string | number | null)[] = ['a', 1, null, 'b', 2]
|
|
43
|
+
* const result = filterDefined(mixed)
|
|
44
|
+
* // Type: (string | number)[]
|
|
45
|
+
*/
|
|
46
|
+
declare function filterDefined<T>(array: readonly (T | null | undefined)[]): Defined<T>[];
|
|
47
|
+
/**
|
|
48
|
+
* Curried version for use in pipelines
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* pipe(
|
|
52
|
+
* users,
|
|
53
|
+
* filterDefinedFn(),
|
|
54
|
+
* map(u => u.name)
|
|
55
|
+
* )
|
|
56
|
+
*/
|
|
57
|
+
declare function filterDefinedFn<T>(): (array: readonly (T | null | undefined)[]) => Defined<T>[];
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Filters an array removing all falsy values with proper type narrowing.
|
|
61
|
+
*
|
|
62
|
+
* Removes: false, 0, 0n, '', null, undefined
|
|
63
|
+
*
|
|
64
|
+
* @example
|
|
65
|
+
* const values: (string | number | null | undefined | false)[] =
|
|
66
|
+
* ['hello', 0, '', null, 'world', false, 42]
|
|
67
|
+
*
|
|
68
|
+
* const truthy = filterTruthy(values)
|
|
69
|
+
* // Type: (string | number)[]
|
|
70
|
+
* // Value: ['hello', 'world', 42]
|
|
71
|
+
*
|
|
72
|
+
* @example
|
|
73
|
+
* // Useful for conditional rendering arrays
|
|
74
|
+
* const items = filterTruthy([
|
|
75
|
+
* showHeader && <Header />,
|
|
76
|
+
* <Content />,
|
|
77
|
+
* showFooter && <Footer />
|
|
78
|
+
* ])
|
|
79
|
+
*/
|
|
80
|
+
declare function filterTruthy<T>(array: readonly T[]): Truthy<T>[];
|
|
81
|
+
/**
|
|
82
|
+
* Curried version for use in pipelines
|
|
83
|
+
*/
|
|
84
|
+
declare function filterTruthyFn<T>(): (array: readonly T[]) => Truthy<T>[];
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Filters an array using a type guard with proper type narrowing.
|
|
88
|
+
*
|
|
89
|
+
* This is the generic solution for filtering with any type guard.
|
|
90
|
+
*
|
|
91
|
+
* @example
|
|
92
|
+
* const items: unknown[] = [1, 'hello', 2, 'world', null]
|
|
93
|
+
*
|
|
94
|
+
* const strings = filterByGuard(items, isString)
|
|
95
|
+
* // Type: string[]
|
|
96
|
+
*
|
|
97
|
+
* const numbers = filterByGuard(items, isNumber)
|
|
98
|
+
* // Type: number[]
|
|
99
|
+
*
|
|
100
|
+
* @example
|
|
101
|
+
* // With custom type guards
|
|
102
|
+
* interface Admin { role: 'admin'; permissions: string[] }
|
|
103
|
+
* interface User { role: 'user' }
|
|
104
|
+
* type Person = Admin | User
|
|
105
|
+
*
|
|
106
|
+
* const isAdmin = (p: Person): p is Admin => p.role === 'admin'
|
|
107
|
+
* const admins = filterByGuard(people, isAdmin)
|
|
108
|
+
* // Type: Admin[]
|
|
109
|
+
*/
|
|
110
|
+
declare function filterByGuard<T, S extends T>(array: readonly T[], guard: TypeGuard<T, S>): S[];
|
|
111
|
+
/**
|
|
112
|
+
* Filters with a refinement function that has access to index and array
|
|
113
|
+
*/
|
|
114
|
+
declare function filterByRefinement<T, S extends T>(array: readonly T[], refinement: Refinement<T, S>): S[];
|
|
115
|
+
/**
|
|
116
|
+
* Creates a filter function from a type guard for use in pipelines
|
|
117
|
+
*
|
|
118
|
+
* @example
|
|
119
|
+
* pipe(
|
|
120
|
+
* items,
|
|
121
|
+
* filterBy(isString),
|
|
122
|
+
* map(s => s.toUpperCase())
|
|
123
|
+
* )
|
|
124
|
+
*/
|
|
125
|
+
declare function filterBy<T, S extends T>(guard: TypeGuard<T, S>): (array: readonly T[]) => S[];
|
|
126
|
+
/**
|
|
127
|
+
* Partitions an array by a type guard into [matches, nonMatches]
|
|
128
|
+
*
|
|
129
|
+
* @example
|
|
130
|
+
* const [strings, others] = partitionByGuard(items, isString)
|
|
131
|
+
* // strings: string[]
|
|
132
|
+
* // others: Exclude<typeof items[number], string>[]
|
|
133
|
+
*/
|
|
134
|
+
declare function partitionByGuard<T, S extends T>(array: readonly T[], guard: TypeGuard<T, S>): [S[], Exclude<T, S>[]];
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Asserts that a value matches a type guard, throwing if it doesn't.
|
|
138
|
+
* This is an assertion function that narrows the type in the current scope.
|
|
139
|
+
*
|
|
140
|
+
* @example
|
|
141
|
+
* function processUser(data: unknown) {
|
|
142
|
+
* assertType(data, isUser, 'Expected user data')
|
|
143
|
+
* // After this line, data is typed as User
|
|
144
|
+
* console.log(data.name)
|
|
145
|
+
* }
|
|
146
|
+
*
|
|
147
|
+
* @example
|
|
148
|
+
* // With custom error
|
|
149
|
+
* assertType(response, isApiResponse, () =>
|
|
150
|
+
* new ValidationError('Invalid API response', response)
|
|
151
|
+
* )
|
|
152
|
+
*/
|
|
153
|
+
declare function assertType<T, S extends T>(value: T, guard: TypeGuard<T, S>, message?: string | (() => Error)): asserts value is S;
|
|
154
|
+
/**
|
|
155
|
+
* Asserts that a value is defined (not null or undefined)
|
|
156
|
+
*
|
|
157
|
+
* @example
|
|
158
|
+
* const user: User | null = findUser(id)
|
|
159
|
+
* assertDefined(user, `User ${id} not found`)
|
|
160
|
+
* // After this line, user is typed as User
|
|
161
|
+
* console.log(user.name)
|
|
162
|
+
*/
|
|
163
|
+
declare function assertDefined<T>(value: T | null | undefined, message?: string): asserts value is T;
|
|
164
|
+
/**
|
|
165
|
+
* Asserts that a value is not null
|
|
166
|
+
*/
|
|
167
|
+
declare function assertNotNull<T>(value: T | null, message?: string): asserts value is T;
|
|
168
|
+
/**
|
|
169
|
+
* Asserts a condition and narrows the type
|
|
170
|
+
*
|
|
171
|
+
* @example
|
|
172
|
+
* function process(value: string | number) {
|
|
173
|
+
* assertCondition(typeof value === 'string', 'Expected string')
|
|
174
|
+
* // value is now string
|
|
175
|
+
* return value.toUpperCase()
|
|
176
|
+
* }
|
|
177
|
+
*/
|
|
178
|
+
declare function assertCondition(condition: boolean, message?: string): asserts condition;
|
|
179
|
+
/**
|
|
180
|
+
* Creates a type assertion function from a type guard
|
|
181
|
+
*
|
|
182
|
+
* @example
|
|
183
|
+
* const assertUser = createAssertion(isUser, 'Expected User')
|
|
184
|
+
*
|
|
185
|
+
* function process(data: unknown) {
|
|
186
|
+
* assertUser(data)
|
|
187
|
+
* // data is now User
|
|
188
|
+
* }
|
|
189
|
+
*/
|
|
190
|
+
declare function createAssertion<T, S extends T>(guard: TypeGuard<T, S>, defaultMessage?: string): (value: T, message?: string) => asserts value is S;
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Narrows a discriminated union by its discriminant property.
|
|
194
|
+
*
|
|
195
|
+
* @example
|
|
196
|
+
* type Event =
|
|
197
|
+
* | { type: 'click'; x: number; y: number }
|
|
198
|
+
* | { type: 'keypress'; key: string }
|
|
199
|
+
* | { type: 'scroll'; delta: number }
|
|
200
|
+
*
|
|
201
|
+
* const event: Event = getEvent()
|
|
202
|
+
*
|
|
203
|
+
* if (narrowUnion(event, 'type', 'click')) {
|
|
204
|
+
* // event is { type: 'click'; x: number; y: number }
|
|
205
|
+
* console.log(event.x, event.y)
|
|
206
|
+
* }
|
|
207
|
+
*/
|
|
208
|
+
declare function narrowUnion<T extends Record<K, PropertyKey>, K extends keyof T, V extends T[K]>(value: T, key: K, expected: V): value is Extract<T, Record<K, V>>;
|
|
209
|
+
/**
|
|
210
|
+
* Creates a type guard for a specific discriminant value
|
|
211
|
+
*
|
|
212
|
+
* @example
|
|
213
|
+
* type Result = { status: 'ok'; data: string } | { status: 'error'; message: string }
|
|
214
|
+
*
|
|
215
|
+
* const isOk = createDiscriminantGuard<Result>()('status', 'ok')
|
|
216
|
+
* const isError = createDiscriminantGuard<Result>()('status', 'error')
|
|
217
|
+
*
|
|
218
|
+
* if (isOk(result)) {
|
|
219
|
+
* console.log(result.data)
|
|
220
|
+
* }
|
|
221
|
+
*/
|
|
222
|
+
declare function createDiscriminantGuard<T extends Record<string, unknown>>(): <K extends keyof T, V extends T[K]>(key: K, value: V) => ((obj: T) => obj is Extract<T, Record<K, V>>);
|
|
223
|
+
/**
|
|
224
|
+
* Exhaustive switch helper for discriminated unions
|
|
225
|
+
*
|
|
226
|
+
* @example
|
|
227
|
+
* type Shape =
|
|
228
|
+
* | { kind: 'circle'; radius: number }
|
|
229
|
+
* | { kind: 'square'; side: number }
|
|
230
|
+
* | { kind: 'rectangle'; width: number; height: number }
|
|
231
|
+
*
|
|
232
|
+
* function area(shape: Shape): number {
|
|
233
|
+
* return switchUnion(shape, 'kind', {
|
|
234
|
+
* circle: (s) => Math.PI * s.radius ** 2,
|
|
235
|
+
* square: (s) => s.side ** 2,
|
|
236
|
+
* rectangle: (s) => s.width * s.height,
|
|
237
|
+
* })
|
|
238
|
+
* }
|
|
239
|
+
*/
|
|
240
|
+
declare function switchUnion<T extends Record<K, string>, K extends keyof T, R>(value: T, key: K, handlers: {
|
|
241
|
+
[V in T[K]]: (value: Extract<T, Record<K, V>>) => R;
|
|
242
|
+
}): R;
|
|
243
|
+
/**
|
|
244
|
+
* Type-safe exhaustive check that ensures all union cases are handled
|
|
245
|
+
*
|
|
246
|
+
* @example
|
|
247
|
+
* type Status = 'pending' | 'success' | 'error'
|
|
248
|
+
*
|
|
249
|
+
* function handleStatus(status: Status) {
|
|
250
|
+
* switch (status) {
|
|
251
|
+
* case 'pending': return 'Loading...'
|
|
252
|
+
* case 'success': return 'Done!'
|
|
253
|
+
* case 'error': return 'Failed'
|
|
254
|
+
* default: return exhaustiveCheck(status)
|
|
255
|
+
* }
|
|
256
|
+
* }
|
|
257
|
+
*/
|
|
258
|
+
declare function exhaustiveCheck(value: never, message?: string): never;
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* Excludes null from a value's type at runtime and compile time.
|
|
262
|
+
* Throws if the value is null.
|
|
263
|
+
*
|
|
264
|
+
* @example
|
|
265
|
+
* const element: HTMLElement | null = document.getElementById('app')
|
|
266
|
+
* const el = excludeNull(element, 'App element not found')
|
|
267
|
+
* // el is HTMLElement
|
|
268
|
+
*/
|
|
269
|
+
declare function excludeNull<T>(value: T | null, message?: string): T;
|
|
270
|
+
/**
|
|
271
|
+
* Excludes undefined from a value's type at runtime and compile time.
|
|
272
|
+
* Throws if the value is undefined.
|
|
273
|
+
*/
|
|
274
|
+
declare function excludeUndefined<T>(value: T | undefined, message?: string): T;
|
|
275
|
+
/**
|
|
276
|
+
* Excludes null and undefined from a value's type.
|
|
277
|
+
* Throws if the value is null or undefined.
|
|
278
|
+
*/
|
|
279
|
+
declare function excludeNullish<T>(value: T | null | undefined, message?: string): T;
|
|
280
|
+
/**
|
|
281
|
+
* Returns the value if defined, otherwise returns the default.
|
|
282
|
+
* Type-safe alternative to the nullish coalescing operator with better inference.
|
|
283
|
+
*
|
|
284
|
+
* @example
|
|
285
|
+
* const config: Config | undefined = loadConfig()
|
|
286
|
+
* const safeConfig = withDefault(config, defaultConfig)
|
|
287
|
+
* // safeConfig is Config (not Config | undefined)
|
|
288
|
+
*/
|
|
289
|
+
declare function withDefault<T>(value: T | null | undefined, defaultValue: T): T;
|
|
290
|
+
/**
|
|
291
|
+
* Returns the value if defined, otherwise calls the factory function.
|
|
292
|
+
* Useful when the default value is expensive to compute.
|
|
293
|
+
*
|
|
294
|
+
* @example
|
|
295
|
+
* const cached = withDefaultLazy(cache.get(key), () => computeExpensiveValue())
|
|
296
|
+
*/
|
|
297
|
+
declare function withDefaultLazy<T>(value: T | null | undefined, factory: () => T): T;
|
|
298
|
+
/**
|
|
299
|
+
* Maps a nullable value, returning null/undefined if the input is null/undefined.
|
|
300
|
+
*
|
|
301
|
+
* @example
|
|
302
|
+
* const user: User | null = findUser(id)
|
|
303
|
+
* const name = mapNullable(user, u => u.name)
|
|
304
|
+
* // name is string | null
|
|
305
|
+
*/
|
|
306
|
+
declare function mapNullable<T, U>(value: T | null | undefined, fn: (value: T) => U): U | null | undefined;
|
|
307
|
+
/**
|
|
308
|
+
* Chains nullable operations, similar to optional chaining but with functions.
|
|
309
|
+
*
|
|
310
|
+
* @example
|
|
311
|
+
* const result = chainNullable(
|
|
312
|
+
* user,
|
|
313
|
+
* u => u.profile,
|
|
314
|
+
* p => p.settings,
|
|
315
|
+
* s => s.theme
|
|
316
|
+
* )
|
|
317
|
+
* // result is Theme | null | undefined
|
|
318
|
+
*/
|
|
319
|
+
declare function chainNullable<T, A>(value: T | null | undefined, fn1: (v: T) => A | null | undefined): A | null | undefined;
|
|
320
|
+
declare function chainNullable<T, A, B>(value: T | null | undefined, fn1: (v: T) => A | null | undefined, fn2: (v: A) => B | null | undefined): B | null | undefined;
|
|
321
|
+
declare function chainNullable<T, A, B, C>(value: T | null | undefined, fn1: (v: T) => A | null | undefined, fn2: (v: A) => B | null | undefined, fn3: (v: B) => C | null | undefined): C | null | undefined;
|
|
322
|
+
declare function chainNullable<T, A, B, C, D>(value: T | null | undefined, fn1: (v: T) => A | null | undefined, fn2: (v: A) => B | null | undefined, fn3: (v: B) => C | null | undefined, fn4: (v: C) => D | null | undefined): D | null | undefined;
|
|
323
|
+
|
|
324
|
+
/**
|
|
325
|
+
* Refines a type by applying multiple type guards in sequence.
|
|
326
|
+
* Each guard further narrows the type.
|
|
327
|
+
*
|
|
328
|
+
* @example
|
|
329
|
+
* const isPositiveNumber = refine(
|
|
330
|
+
* isNumber,
|
|
331
|
+
* (n): n is number => n > 0
|
|
332
|
+
* )
|
|
333
|
+
*
|
|
334
|
+
* if (isPositiveNumber(value)) {
|
|
335
|
+
* // value is number and > 0
|
|
336
|
+
* }
|
|
337
|
+
*/
|
|
338
|
+
declare function refine<T, S extends T, R extends S>(guard1: TypeGuard<T, S>, guard2: TypeGuard<S, R>): TypeGuard<T, R>;
|
|
339
|
+
declare function refine<T, S extends T, R extends S, Q extends R>(guard1: TypeGuard<T, S>, guard2: TypeGuard<S, R>, guard3: TypeGuard<R, Q>): TypeGuard<T, Q>;
|
|
340
|
+
/**
|
|
341
|
+
* Combines multiple type guards with OR logic.
|
|
342
|
+
* Returns true if any guard matches.
|
|
343
|
+
*
|
|
344
|
+
* @example
|
|
345
|
+
* const isStringOrNumber = oneOf(isString, isNumber)
|
|
346
|
+
*
|
|
347
|
+
* if (isStringOrNumber(value)) {
|
|
348
|
+
* // value is string | number
|
|
349
|
+
* }
|
|
350
|
+
*/
|
|
351
|
+
declare function oneOf<T, S1 extends T>(guard1: TypeGuard<T, S1>): TypeGuard<T, S1>;
|
|
352
|
+
declare function oneOf<T, S1 extends T, S2 extends T>(guard1: TypeGuard<T, S1>, guard2: TypeGuard<T, S2>): TypeGuard<T, S1 | S2>;
|
|
353
|
+
declare function oneOf<T, S1 extends T, S2 extends T, S3 extends T>(guard1: TypeGuard<T, S1>, guard2: TypeGuard<T, S2>, guard3: TypeGuard<T, S3>): TypeGuard<T, S1 | S2 | S3>;
|
|
354
|
+
declare function oneOf<T, S1 extends T, S2 extends T, S3 extends T, S4 extends T>(guard1: TypeGuard<T, S1>, guard2: TypeGuard<T, S2>, guard3: TypeGuard<T, S3>, guard4: TypeGuard<T, S4>): TypeGuard<T, S1 | S2 | S3 | S4>;
|
|
355
|
+
/**
|
|
356
|
+
* Combines multiple type guards with AND logic.
|
|
357
|
+
* Returns true only if all guards match.
|
|
358
|
+
*
|
|
359
|
+
* @example
|
|
360
|
+
* const isNonEmptyString = allOf(
|
|
361
|
+
* isString,
|
|
362
|
+
* (s): s is string => s.length > 0
|
|
363
|
+
* )
|
|
364
|
+
*/
|
|
365
|
+
declare function allOf<T, S1 extends T>(guard1: TypeGuard<T, S1>): TypeGuard<T, S1>;
|
|
366
|
+
declare function allOf<T, S1 extends T, S2 extends S1>(guard1: TypeGuard<T, S1>, guard2: TypeGuard<S1, S2>): TypeGuard<T, S2>;
|
|
367
|
+
declare function allOf<T, S1 extends T, S2 extends S1, S3 extends S2>(guard1: TypeGuard<T, S1>, guard2: TypeGuard<S1, S2>, guard3: TypeGuard<S2, S3>): TypeGuard<T, S3>;
|
|
368
|
+
/**
|
|
369
|
+
* Negates a type guard.
|
|
370
|
+
*
|
|
371
|
+
* @example
|
|
372
|
+
* const isNotNull = not(isNull)
|
|
373
|
+
* const items = array.filter(isNotNull)
|
|
374
|
+
*/
|
|
375
|
+
declare function not<T, S extends T>(guard: TypeGuard<T, S>): TypeGuard<T, Exclude<T, S>>;
|
|
376
|
+
/**
|
|
377
|
+
* Creates a type guard for array elements.
|
|
378
|
+
*
|
|
379
|
+
* @example
|
|
380
|
+
* const isStringArray = arrayOf(isString)
|
|
381
|
+
*
|
|
382
|
+
* if (isStringArray(value)) {
|
|
383
|
+
* // value is string[]
|
|
384
|
+
* }
|
|
385
|
+
*/
|
|
386
|
+
declare function arrayOf<T, S extends T>(guard: TypeGuard<T, S>): TypeGuard<unknown, S[]>;
|
|
387
|
+
/**
|
|
388
|
+
* Creates a type guard for objects with specific shape.
|
|
389
|
+
*
|
|
390
|
+
* @example
|
|
391
|
+
* const isUser = objectOf({
|
|
392
|
+
* id: isNumber,
|
|
393
|
+
* name: isString,
|
|
394
|
+
* email: isString
|
|
395
|
+
* })
|
|
396
|
+
*
|
|
397
|
+
* if (isUser(data)) {
|
|
398
|
+
* // data is { id: number; name: string; email: string }
|
|
399
|
+
* }
|
|
400
|
+
*/
|
|
401
|
+
declare function objectOf<T extends Record<string, TypeGuard<unknown, unknown>>>(shape: T): TypeGuard<unknown, {
|
|
402
|
+
[K in keyof T]: T[K] extends TypeGuard<unknown, infer S> ? S : never;
|
|
403
|
+
}>;
|
|
404
|
+
/**
|
|
405
|
+
* Creates a type guard for literal values.
|
|
406
|
+
*
|
|
407
|
+
* @example
|
|
408
|
+
* const isAdmin = literal('admin')
|
|
409
|
+
* const isStatus = oneOf(literal('pending'), literal('done'), literal('error'))
|
|
410
|
+
*/
|
|
411
|
+
declare function literal<T extends string | number | boolean>(expected: T): TypeGuard<unknown, T>;
|
|
412
|
+
/**
|
|
413
|
+
* Creates a type guard for optional properties.
|
|
414
|
+
*
|
|
415
|
+
* @example
|
|
416
|
+
* const isOptionalString = optional(isString)
|
|
417
|
+
* // Matches string | undefined
|
|
418
|
+
*/
|
|
419
|
+
declare function optional<T, S extends T>(guard: TypeGuard<T, S>): TypeGuard<T | undefined, S | undefined>;
|
|
420
|
+
/**
|
|
421
|
+
* Creates a type guard for nullable properties.
|
|
422
|
+
*
|
|
423
|
+
* @example
|
|
424
|
+
* const isNullableString = nullable(isString)
|
|
425
|
+
* // Matches string | null
|
|
426
|
+
*/
|
|
427
|
+
declare function nullable<T, S extends T>(guard: TypeGuard<T, S>): TypeGuard<T | null, S | null>;
|
|
428
|
+
|
|
429
|
+
export { type Defined, type Falsy, type GuardedType, type Refinement, type Truthy, type TypeGuard, allOf, arrayOf, assertCondition, assertDefined, assertNotNull, assertType, chainNullable, createAssertion, createDiscriminantGuard, excludeNull, excludeNullish, excludeUndefined, exhaustiveCheck, filterBy, filterByGuard, filterByRefinement, filterDefined, filterDefinedFn, filterTruthy, filterTruthyFn, literal, mapNullable, narrowUnion, not, nullable, objectOf, oneOf, optional, partitionByGuard, refine, switchUnion, withDefault, withDefaultLazy };
|