is-kit 1.7.0 → 1.7.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/dist/index.d.mts CHANGED
@@ -50,6 +50,17 @@ type InferSchema<S extends Schema> = Simplify<{
50
50
  readonly [K in OptionalSchemaKeys<S>]?: InferSchemaField<S[K]>;
51
51
  }>;
52
52
 
53
+ /**
54
+ * Asserts that a value satisfies a guard or refinement, otherwise throws.
55
+ *
56
+ * @param guard Guard/refinement to evaluate.
57
+ * @param value Value to assert.
58
+ * @param message Optional error message when assertion fails.
59
+ * @returns Nothing when assertion passes; throws an `Error` on failure.
60
+ */
61
+ declare function assert<T>(guard: Guard<T>, value: unknown, message?: string): asserts value is T;
62
+ declare function assert<A, B extends A>(refine: Refine<A, B>, value: A, message?: string): asserts value is B;
63
+
53
64
  /**
54
65
  * Wraps a user function as a typed predicate.
55
66
  *
@@ -63,23 +74,58 @@ declare function define<T>(fn: (value: unknown) => value is T): Predicate<T>;
63
74
  declare function define<T>(fn: (value: unknown) => boolean): Predicate<T>;
64
75
 
65
76
  /**
66
- * Converts a boolean predicate into a refinement preserving the input type.
77
+ * Creates a guard that matches values equal to the provided target using `Object.is` semantics.
67
78
  *
68
- * @param fn Boolean-returning function over a value of type A.
69
- * @returns Refinement equivalent to the provided predicate.
79
+ * @param target Target value to compare against.
80
+ * @returns Predicate narrowing to the exact literal type of `target`.
70
81
  */
71
- declare const predicateToRefine: <A>(fn: (value: A) => boolean) => Refine<A, A>;
82
+ declare function equals<const T>(target: T): Predicate<T>;
83
+ /**
84
+ * Creates a comparator builder based on a guard and selector.
85
+ * Useful for checking whether a selected key or projection equals a target value.
86
+ *
87
+ * @param guard Guard for the base type before selecting a field.
88
+ * @param selector Optional selector to project the comparable key; if omitted, returns a function to supply it later.
89
+ * @returns Function that accepts a target and returns a predicate over the original value.
90
+ */
91
+ declare function equalsBy<F extends Predicate<unknown>>(guard: F): <K>(selector: (value: GuardedOf<F>) => K) => <const T extends K>(target: T) => Predicate<GuardedOf<F>>;
92
+ declare function equalsBy<F extends Predicate<unknown>, K>(guard: F, selector: (value: GuardedOf<F>) => K): <const T extends K>(target: T) => Predicate<GuardedOf<F>>;
93
+ /**
94
+ * Creates a guard that matches objects having a key equal to the target value.
95
+ *
96
+ * @param key Property key to compare.
97
+ * @param target Value to compare using `Object.is`.
98
+ * @returns Predicate narrowing to objects where `key` exists and equals `target`.
99
+ */
100
+ declare function equalsKey<K extends PropertyKey, const T>(key: K, target: T): <A extends Record<K, unknown>>(input: unknown) => input is A & Record<K, T>;
72
101
 
73
102
  /**
74
- * Asserts that a value satisfies a guard or refinement, otherwise throws.
103
+ * Checks whether a value has the specified own key.
75
104
  *
76
- * @param guard Guard/refinement to evaluate.
77
- * @param value Value to assert.
78
- * @param message Optional error message when assertion fails.
79
- * @returns Nothing when assertion passes; throws an `Error` on failure.
105
+ * @param key Property key to check.
106
+ * @returns Predicate narrowing to an object with the key present.
80
107
  */
81
- declare function assert<T>(guard: Guard<T>, value: unknown, message?: string): asserts value is T;
82
- declare function assert<A, B extends A>(refine: Refine<A, B>, value: A, message?: string): asserts value is B;
108
+ declare const hasKey: <K extends PropertyKey>(key: K) => Predicate<Record<K, unknown>>;
109
+ /**
110
+ * Checks whether a value has all specified own keys.
111
+ *
112
+ * @param keys Property keys to check.
113
+ * @returns Predicate narrowing to an object with all keys present.
114
+ */
115
+ declare const hasKeys: <const KS extends readonly [PropertyKey, ...PropertyKey[]]>(...keys: KS) => Predicate<Record<KS[number], unknown>>;
116
+ /**
117
+ * Builds a guard that narrows a specific key to the provided literal value.
118
+ *
119
+ * Given a base guard and a property key, returns a function that accepts a
120
+ * target value and produces a guard of the base type intersected with
121
+ * `{ [key]: target }`.
122
+ *
123
+ * @param guard Base guard for objects that include `key`.
124
+ * @param key Property key to narrow.
125
+ * @returns Builder that takes a literal `target` and returns a guard that
126
+ * narrows `key` to that literal.
127
+ */
128
+ declare function narrowKeyTo<A, K extends keyof A>(guard: Guard<A>, key: K): <const T extends A[K]>(target: T) => Predicate<A & Record<K, T>>;
83
129
 
84
130
  /**
85
131
  * Combines a precondition guard with an additional refinement to narrow the type.
@@ -162,32 +208,6 @@ declare function optional<A, B extends A>(refine: Refine<A, B>): Refine<A | unde
162
208
  declare function required<T>(guard: Guard<T | undefined>): Guard<T>;
163
209
  declare function required<A, B extends A>(refine: Refine<A | undefined, B | undefined>): Refine<A, B>;
164
210
 
165
- /**
166
- * Creates a guard that matches values equal to the provided target using `Object.is` semantics.
167
- *
168
- * @param target Target value to compare against.
169
- * @returns Predicate narrowing to the exact literal type of `target`.
170
- */
171
- declare function equals<const T>(target: T): Predicate<T>;
172
- /**
173
- * Creates a comparator builder based on a guard and selector.
174
- * Useful for checking whether a selected key or projection equals a target value.
175
- *
176
- * @param guard Guard for the base type before selecting a field.
177
- * @param selector Optional selector to project the comparable key; if omitted, returns a function to supply it later.
178
- * @returns Function that accepts a target and returns a predicate over the original value.
179
- */
180
- declare function equalsBy<F extends Predicate<unknown>>(guard: F): <K>(selector: (value: GuardedOf<F>) => K) => <const T extends K>(target: T) => Predicate<GuardedOf<F>>;
181
- declare function equalsBy<F extends Predicate<unknown>, K>(guard: F, selector: (value: GuardedOf<F>) => K): <const T extends K>(target: T) => Predicate<GuardedOf<F>>;
182
- /**
183
- * Creates a guard that matches objects having a key equal to the target value.
184
- *
185
- * @param key Property key to compare.
186
- * @param target Value to compare using `Object.is`.
187
- * @returns Predicate narrowing to objects where `key` exists and equals `target`.
188
- */
189
- declare function equalsKey<K extends PropertyKey, const T>(key: K, target: T): <A extends Record<K, unknown>>(input: unknown) => input is A & Record<K, T>;
190
-
191
211
  /**
192
212
  * Executes a guard or refinement and returns a tagged result with the value when valid.
193
213
  *
@@ -207,6 +227,14 @@ declare function safeParse(fn: (value: unknown) => boolean, value: unknown): Par
207
227
  declare function safeParseWith<T>(guard: Guard<T>): (value: unknown) => ParseResult<T>;
208
228
  declare function safeParseWith<A, B extends A>(refine: Refine<A, B>): (value: A) => ParseResult<B>;
209
229
 
230
+ /**
231
+ * Converts a boolean predicate into a refinement preserving the input type.
232
+ *
233
+ * @param fn Boolean-returning function over a value of type A.
234
+ * @returns Refinement equivalent to the provided predicate.
235
+ */
236
+ declare const predicateToRefine: <A>(fn: (value: A) => boolean) => Refine<A, A>;
237
+
210
238
  /**
211
239
  * Checks whether a value is a string.
212
240
  *
@@ -317,101 +345,6 @@ declare const isNull: Predicate<null>;
317
345
  */
318
346
  declare const isPrimitive: Guard<string | number | bigint | boolean | symbol | null | undefined>;
319
347
 
320
- /**
321
- * Validates an array where every element satisfies the provided guard.
322
- *
323
- * @param elementGuard Guard applied to each array element.
324
- * @returns Predicate narrowing to a readonly array of the guarded element type.
325
- */
326
- declare function arrayOf<F extends Predicate<unknown>>(elementGuard: F): Predicate<readonly GuardedOf<F>[]>;
327
- /**
328
- * Validates a non-empty array where every element satisfies the provided guard.
329
- *
330
- * @param elementGuard Guard applied to each array element.
331
- * @returns Predicate narrowing to a readonly non-empty array of the guarded element type.
332
- */
333
- declare function nonEmptyArrayOf<F extends Predicate<unknown>>(elementGuard: F): Predicate<readonly [GuardedOf<F>, ...GuardedOf<F>[]]>;
334
-
335
- /**
336
- * Validates a set where every value satisfies the provided guard.
337
- *
338
- * @param valueGuard Guard applied to each set value.
339
- * @returns Predicate narrowing to a readonly set of the guarded value type.
340
- */
341
- declare function setOf<VF extends Predicate<unknown>>(valueGuard: VF): Predicate<ReadonlySet<GuardedOf<VF>>>;
342
-
343
- /**
344
- * Validates a map where every key and value satisfies the provided guards.
345
- *
346
- * @param keyGuard Guard applied to each map key.
347
- * @param valueGuard Guard applied to each map value.
348
- * @returns Predicate narrowing to a readonly map with guarded key/value types.
349
- */
350
- declare function mapOf<KF extends Predicate<unknown>, VF extends Predicate<unknown>>(keyGuard: KF, valueGuard: VF): Predicate<ReadonlyMap<GuardedOf<KF>, GuardedOf<VF>>>;
351
-
352
- /**
353
- * Validates a fixed-length tuple by applying element-wise guards.
354
- *
355
- * @param guards Guards corresponding to each tuple position.
356
- * @returns Predicate that narrows to a readonly tuple of guarded element types.
357
- */
358
- declare function tupleOf<const Fs extends readonly Predicate<unknown>[]>(...guards: Fs): Predicate<{
359
- readonly [K in keyof Fs]: GuardedOf<Fs[K]>;
360
- }>;
361
-
362
- /**
363
- * Combines multiple guards; passes when any one guard matches.
364
- *
365
- * @param guards One or more type guards/refinements to try.
366
- * @returns Predicate that narrows to the union of guarded types.
367
- */
368
- declare function oneOf<Fs extends readonly Predicate<unknown>[]>(...guards: Fs): Predicate<GuardedOf<Fs[number]>>;
369
- declare function oneOf<A, Fs extends readonly Predicate<A>[]>(...guards: Fs): (input: A) => input is GuardedWithin<Fs, A>;
370
-
371
- /**
372
- * Validates a record-like object by guarding both keys and values.
373
- *
374
- * @param keyFunction Guard for string keys (e.g., specific literal keys pattern).
375
- * @param valueFunction Guard applied to each value in the record.
376
- * @returns Predicate narrowing to a readonly record with guarded key/value types.
377
- */
378
- declare function recordOf<KF extends Predicate<string>, VF extends Predicate<unknown>>(keyFunction: KF, valueFunction: VF): Predicate<Readonly<Record<GuardedOf<KF>, GuardedOf<VF>>>>;
379
-
380
- /**
381
- * Marks a struct schema field as optional at the key level.
382
- *
383
- * When used inside `struct`, the property may be absent. If the property exists,
384
- * its value must satisfy the wrapped guard.
385
- *
386
- * @param guard Guard used to validate the property value when the key exists.
387
- * @returns Schema field marker understood by `struct`.
388
- */
389
- declare function optionalKey<G extends Predicate<unknown>>(guard: G): OptionalSchemaField<G>;
390
- /**
391
- * Validates an object against a field-to-guard schema.
392
- * Keys in the schema are required unless wrapped with `optionalKey`; optionally
393
- * rejects extra keys when `exact: true`.
394
- *
395
- * @param schema Record of property guards.
396
- * @param options When `{ exact: true }`, disallows properties not in `schema`.
397
- * @returns Predicate that narrows to the inferred struct type.
398
- */
399
- declare function struct<S extends Schema>(schema: S, options?: {
400
- exact?: boolean;
401
- }): Predicate<InferSchema<S>>;
402
-
403
- /**
404
- * Creates a guard that matches when the input is one of the provided literal values.
405
- * Uses `Object.is` for exact value semantics. For large sets, keeps O(1) lookups while
406
- * preserving `+0` vs `-0` by tracking zero variants explicitly in addition to a `Set`.
407
- *
408
- * @param values Literal primitives to compare against (varargs or single readonly tuple array).
409
- * @returns Predicate narrowing to the union of the provided literal values.
410
- */
411
- declare function oneOfValues<const T extends ReadonlyArray<Primitive>>(...values: T): Predicate<T[number]>;
412
- declare function oneOfValues<const T extends ReadonlyArray<Primitive>>(values: T): Predicate<T[number]>;
413
- declare function oneOfValues<const T extends ReadonlyArray<Primitive>>(...valuesOrArray: T | [T]): Predicate<T[number]>;
414
-
415
348
  /**
416
349
  * Checks whether a value is a function.
417
350
  *
@@ -535,32 +468,99 @@ declare const isBlob: Predicate<Blob>;
535
468
  declare const isInstanceOf: <C extends abstract new (...args: unknown[]) => unknown>(constructor: C) => Guard<InstanceType<C>>;
536
469
 
537
470
  /**
538
- * Checks whether a value has the specified own key.
471
+ * Validates an array where every element satisfies the provided guard.
539
472
  *
540
- * @param key Property key to check.
541
- * @returns Predicate narrowing to an object with the key present.
473
+ * @param elementGuard Guard applied to each array element.
474
+ * @returns Predicate narrowing to a readonly array of the guarded element type.
542
475
  */
543
- declare const hasKey: <K extends PropertyKey>(key: K) => Predicate<Record<K, unknown>>;
476
+ declare function arrayOf<F extends Predicate<unknown>>(elementGuard: F): Predicate<readonly GuardedOf<F>[]>;
544
477
  /**
545
- * Checks whether a value has all specified own keys.
478
+ * Validates a non-empty array where every element satisfies the provided guard.
546
479
  *
547
- * @param keys Property keys to check.
548
- * @returns Predicate narrowing to an object with all keys present.
480
+ * @param elementGuard Guard applied to each array element.
481
+ * @returns Predicate narrowing to a readonly non-empty array of the guarded element type.
549
482
  */
550
- declare const hasKeys: <const KS extends readonly [PropertyKey, ...PropertyKey[]]>(...keys: KS) => Predicate<Record<KS[number], unknown>>;
483
+ declare function nonEmptyArrayOf<F extends Predicate<unknown>>(elementGuard: F): Predicate<readonly [GuardedOf<F>, ...GuardedOf<F>[]]>;
484
+
551
485
  /**
552
- * Builds a guard that narrows a specific key to the provided literal value.
486
+ * Validates a map where every key and value satisfies the provided guards.
553
487
  *
554
- * Given a base guard and a property key, returns a function that accepts a
555
- * target value and produces a guard of the base type intersected with
556
- * `{ [key]: target }`.
488
+ * @param keyGuard Guard applied to each map key.
489
+ * @param valueGuard Guard applied to each map value.
490
+ * @returns Predicate narrowing to a readonly map with guarded key/value types.
491
+ */
492
+ declare function mapOf<KF extends Predicate<unknown>, VF extends Predicate<unknown>>(keyGuard: KF, valueGuard: VF): Predicate<ReadonlyMap<GuardedOf<KF>, GuardedOf<VF>>>;
493
+
494
+ /**
495
+ * Combines multiple guards; passes when any one guard matches.
557
496
  *
558
- * @param guard Base guard for objects that include `key`.
559
- * @param key Property key to narrow.
560
- * @returns Builder that takes a literal `target` and returns a guard that
561
- * narrows `key` to that literal.
497
+ * @param guards One or more type guards/refinements to try.
498
+ * @returns Predicate that narrows to the union of guarded types.
562
499
  */
563
- declare function narrowKeyTo<A, K extends keyof A>(guard: Guard<A>, key: K): <const T extends A[K]>(target: T) => Predicate<A & Record<K, T>>;
500
+ declare function oneOf<Fs extends readonly Predicate<unknown>[]>(...guards: Fs): Predicate<GuardedOf<Fs[number]>>;
501
+ declare function oneOf<A, Fs extends readonly Predicate<A>[]>(...guards: Fs): (input: A) => input is GuardedWithin<Fs, A>;
502
+
503
+ /**
504
+ * Creates a guard that matches when the input is one of the provided literal values.
505
+ * Uses `Object.is` for exact value semantics. For large sets, keeps O(1) lookups while
506
+ * preserving `+0` vs `-0` by tracking zero variants explicitly in addition to a `Set`.
507
+ *
508
+ * @param values Literal primitives to compare against (varargs or single readonly tuple array).
509
+ * @returns Predicate narrowing to the union of the provided literal values.
510
+ */
511
+ declare function oneOfValues<const T extends ReadonlyArray<Primitive>>(...values: T): Predicate<T[number]>;
512
+ declare function oneOfValues<const T extends ReadonlyArray<Primitive>>(values: T): Predicate<T[number]>;
513
+ declare function oneOfValues<const T extends ReadonlyArray<Primitive>>(...valuesOrArray: T | [T]): Predicate<T[number]>;
514
+
515
+ /**
516
+ * Validates a record-like object by guarding both keys and values.
517
+ *
518
+ * @param keyFunction Guard for string keys (e.g., specific literal keys pattern).
519
+ * @param valueFunction Guard applied to each value in the record.
520
+ * @returns Predicate narrowing to a readonly record with guarded key/value types.
521
+ */
522
+ declare function recordOf<KF extends Predicate<string>, VF extends Predicate<unknown>>(keyFunction: KF, valueFunction: VF): Predicate<Readonly<Record<GuardedOf<KF>, GuardedOf<VF>>>>;
523
+
524
+ /**
525
+ * Validates a set where every value satisfies the provided guard.
526
+ *
527
+ * @param valueGuard Guard applied to each set value.
528
+ * @returns Predicate narrowing to a readonly set of the guarded value type.
529
+ */
530
+ declare function setOf<VF extends Predicate<unknown>>(valueGuard: VF): Predicate<ReadonlySet<GuardedOf<VF>>>;
531
+
532
+ /**
533
+ * Marks a struct schema field as optional at the key level.
534
+ *
535
+ * When used inside `struct`, the property may be absent. If the property exists,
536
+ * its value must satisfy the wrapped guard.
537
+ *
538
+ * @param guard Guard used to validate the property value when the key exists.
539
+ * @returns Schema field marker understood by `struct`.
540
+ */
541
+ declare function optionalKey<G extends Predicate<unknown>>(guard: G): OptionalSchemaField<G>;
542
+ /**
543
+ * Validates an object against a field-to-guard schema.
544
+ * Keys in the schema are required unless wrapped with `optionalKey`; optionally
545
+ * rejects extra keys when `exact: true`.
546
+ *
547
+ * @param schema Record of property guards.
548
+ * @param options When `{ exact: true }`, disallows properties not in `schema`.
549
+ * @returns Predicate that narrows to the inferred struct type.
550
+ */
551
+ declare function struct<S extends Schema>(schema: S, options?: {
552
+ exact?: boolean;
553
+ }): Predicate<InferSchema<S>>;
554
+
555
+ /**
556
+ * Validates a fixed-length tuple by applying element-wise guards.
557
+ *
558
+ * @param guards Guards corresponding to each tuple position.
559
+ * @returns Predicate that narrows to a readonly tuple of guarded element types.
560
+ */
561
+ declare function tupleOf<const Fs extends readonly Predicate<unknown>[]>(...guards: Fs): Predicate<{
562
+ readonly [K in keyof Fs]: GuardedOf<Fs[K]>;
563
+ }>;
564
564
 
565
565
  /**
566
566
  * Checks whether every array element satisfies the provided predicate.