is-kit 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +112 -0
- package/dist/index.d.mts +390 -0
- package/dist/index.d.ts +390 -0
- package/dist/index.js +355 -0
- package/dist/index.mjs +279 -0
- package/package.json +66 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 @nyaomaru
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
# is-kit
|
|
2
|
+
|
|
3
|
+
<p align="center">
|
|
4
|
+
<img src="https://raw.githubusercontent.com/nyaomaru/is-kit/main/docs/public/iskit_logo1.svg" width="200px" align="center" alt="is-kit logo" />
|
|
5
|
+
</p>
|
|
6
|
+
|
|
7
|
+
<p align="center">
|
|
8
|
+
<a href="(https://www.npmjs.com/package/is-kit">
|
|
9
|
+
<img src="https://img.shields.io/npm/v/is-kit.svg" alt="npm version">
|
|
10
|
+
</a>
|
|
11
|
+
<a href="https://jsr.io/@nyaomaru/is-kit">
|
|
12
|
+
<img src="https://img.shields.io/jsr/v/@nyaomaru/is-kit" alt="JSR">
|
|
13
|
+
</a>
|
|
14
|
+
<a href="https://github.com/nyaomaru/is-kit/blob/main/LICENSE">
|
|
15
|
+
<img src="https://img.shields.io/npm/l/@nyaomaru/is-kit.svg?sanitize=true" alt="License">
|
|
16
|
+
</a>
|
|
17
|
+
</p>
|
|
18
|
+
|
|
19
|
+
Lightweight, zero-dependency toolkit for building `isFoo` style type guards in TypeScript.
|
|
20
|
+
Runtime-safe 🛡️, composable 🧩, and ergonomic ✨.
|
|
21
|
+
|
|
22
|
+
[👉 See full document](https://is-kit-docs.vercel.app/)
|
|
23
|
+
|
|
24
|
+
## Why?
|
|
25
|
+
|
|
26
|
+
Tired of writing the same `isFoo` again and again?
|
|
27
|
+
Let `is-kit` do it for you:
|
|
28
|
+
|
|
29
|
+
- Less boilerplate
|
|
30
|
+
- Type-safe
|
|
31
|
+
- Composable
|
|
32
|
+
- Zero-dependency
|
|
33
|
+
|
|
34
|
+
> ☕ Grab a coffee, let `is-kit` do the boring work.
|
|
35
|
+
|
|
36
|
+
## Install
|
|
37
|
+
|
|
38
|
+
Node via npm
|
|
39
|
+
|
|
40
|
+
```bash
|
|
41
|
+
pnpm add is-kit
|
|
42
|
+
# or
|
|
43
|
+
bun add is-kit
|
|
44
|
+
# or
|
|
45
|
+
npm i is-kit
|
|
46
|
+
# or
|
|
47
|
+
yarn add is-kit
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
ESM and CJS builds are provided for npm consumers. Types are bundled.
|
|
51
|
+
|
|
52
|
+
### JSR (TypeScript source)
|
|
53
|
+
|
|
54
|
+
```ts
|
|
55
|
+
// Deno/Bun/JSR-aware tooling
|
|
56
|
+
import { define, and, or } from 'jsr:@nyaomaru/is-kit';
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
## Quick start
|
|
60
|
+
|
|
61
|
+
Build `is` functions from tiny, composable pieces:
|
|
62
|
+
|
|
63
|
+
```ts
|
|
64
|
+
import {
|
|
65
|
+
define,
|
|
66
|
+
predicateToRefine,
|
|
67
|
+
and,
|
|
68
|
+
or,
|
|
69
|
+
not,
|
|
70
|
+
isString,
|
|
71
|
+
isNumber,
|
|
72
|
+
} from 'is-kit';
|
|
73
|
+
|
|
74
|
+
// 1. define small pieces
|
|
75
|
+
const isShortString = define<string>(
|
|
76
|
+
(value) => isString(value) && value.length <= 3
|
|
77
|
+
);
|
|
78
|
+
const isPositive = predicateToRefine<number>((num) => num > 0);
|
|
79
|
+
|
|
80
|
+
// 2. compose them
|
|
81
|
+
const isPositiveNumber = and(isNumber, isPositive);
|
|
82
|
+
const shortOrPositive = or(isShortString, isPositiveNumber);
|
|
83
|
+
const isOther = not(shortOrPositive);
|
|
84
|
+
|
|
85
|
+
// 3. use them
|
|
86
|
+
shortOrPositive('foo'); // true
|
|
87
|
+
shortOrPositive(42); // true
|
|
88
|
+
isOther(false); // true
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
These primitives plug into higher-level combinators like `struct` when you need to shape objects.
|
|
92
|
+
|
|
93
|
+
## Core Ideas
|
|
94
|
+
|
|
95
|
+
- **Define once**: `define<T>(fn)` turns a plain function into a type guard.
|
|
96
|
+
- **Upgrade predicates**: `predicateToRefine(fn)` adds narrowing.
|
|
97
|
+
- **Compose freely**: `and`, `or`, `not`, `oneOf`, `arrayOf`, `struct` …
|
|
98
|
+
- **Stay ergonomic**: helpers like `nullable`, `optional`, `equals`, `safeParse`.
|
|
99
|
+
|
|
100
|
+
## API Reference
|
|
101
|
+
|
|
102
|
+
👉 See the docs app under `docs/` (API Reference section). Each helper is documented with runnable examples.
|
|
103
|
+
|
|
104
|
+
## Development
|
|
105
|
+
|
|
106
|
+
Requires Node 22 and pnpm 10.12.4 (via Corepack or mise).
|
|
107
|
+
Want to hack on `is-kit`?
|
|
108
|
+
See `DEVELOPER.md` for setup, scripts, and contribution workflow.
|
|
109
|
+
|
|
110
|
+
## Contributing
|
|
111
|
+
|
|
112
|
+
See `CONTRIBUTE.md` for workflow, commit style, and tool setup. 🚀
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,390 @@
|
|
|
1
|
+
type Predicate<T> = (value: unknown) => value is T;
|
|
2
|
+
type Refinement<A, B extends A> = (value: A) => value is B;
|
|
3
|
+
type Guard<T> = Predicate<T>;
|
|
4
|
+
type Refine<A, B extends A> = Refinement<A, B>;
|
|
5
|
+
type RefineChain<In, T extends readonly unknown[]> = T extends [] ? [] : T extends [Refine<In, infer Next>, ...infer R] ? [Refine<In, Next>, ...RefineChain<Next, Extract<R, readonly unknown[]>>] : never;
|
|
6
|
+
type ChainResult<In, T extends readonly unknown[]> = T extends [] ? In : T extends [Refine<In, infer Next>, ...infer R] ? ChainResult<Next, Extract<R, readonly unknown[]>> : never;
|
|
7
|
+
type OutOfGuards<T extends readonly Guard<unknown>[]> = T[number] extends Guard<infer U> ? U : never;
|
|
8
|
+
type ParseResult<T> = {
|
|
9
|
+
valid: true;
|
|
10
|
+
value: T;
|
|
11
|
+
} | {
|
|
12
|
+
valid: false;
|
|
13
|
+
};
|
|
14
|
+
type GuardedOf<F> = F extends (value: unknown) => value is infer G ? G : never;
|
|
15
|
+
type GuardedWithin<Fs, A> = Extract<Fs extends readonly unknown[] ? GuardedOf<Fs[number]> : never, A>;
|
|
16
|
+
type Schema = Readonly<Record<string, Predicate<unknown>>>;
|
|
17
|
+
type InferSchema<S extends Schema> = {
|
|
18
|
+
readonly [K in keyof S]: GuardedOf<S[K]>;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
type Primitive = string | number | boolean | bigint | symbol | null | undefined;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Wraps a user function as a typed predicate.
|
|
25
|
+
*
|
|
26
|
+
* Note: The correctness of the predicate is the caller's responsibility.
|
|
27
|
+
* Its result is coerced to a boolean using `!!` for consistent guard behavior.
|
|
28
|
+
*
|
|
29
|
+
* @param fn Function that returns truthy when the value matches the target shape.
|
|
30
|
+
* @returns Predicate narrowing to the intended type when it returns true.
|
|
31
|
+
*/
|
|
32
|
+
declare function define<T>(fn: (value: unknown) => value is T): Predicate<T>;
|
|
33
|
+
declare function define<T>(fn: (value: unknown) => boolean): Predicate<T>;
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Converts a boolean predicate into a refinement preserving the input type.
|
|
37
|
+
*
|
|
38
|
+
* @param fn Boolean-returning function over a value of type A.
|
|
39
|
+
* @returns Refinement equivalent to the provided predicate.
|
|
40
|
+
*/
|
|
41
|
+
declare const predicateToRefine: <A>(fn: (value: A) => boolean) => Refine<A, A>;
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Combines a precondition guard with an additional refinement to narrow the type.
|
|
45
|
+
*
|
|
46
|
+
* @param precondition Broad guard evaluated first; short-circuits on failure.
|
|
47
|
+
* @param condition Refinement evaluated only when `precondition` passes.
|
|
48
|
+
* @returns Predicate that narrows the input to a subtype.
|
|
49
|
+
*/
|
|
50
|
+
declare function and<A, B extends A>(precondition: Guard<A>, condition: Refine<A, B>): Predicate<B>;
|
|
51
|
+
/**
|
|
52
|
+
* Chains a sequence of refinements after a precondition, returning the final guard type.
|
|
53
|
+
*
|
|
54
|
+
* @param precondition Initial guard applied first.
|
|
55
|
+
* @param steps Subsequent refinements applied in order.
|
|
56
|
+
* @returns Guard reflecting the result of the refinement chain.
|
|
57
|
+
*/
|
|
58
|
+
declare function andAll<A>(precondition: Guard<A>): Guard<A>;
|
|
59
|
+
declare function andAll<A, B extends A>(precondition: Guard<A>, step1: Refine<A, B>): Guard<B>;
|
|
60
|
+
declare function andAll<A, B extends A, C extends B>(precondition: Guard<A>, step1: Refine<A, B>, step2: Refine<B, C>): Guard<C>;
|
|
61
|
+
/**
|
|
62
|
+
* Logical OR over multiple guards.
|
|
63
|
+
*
|
|
64
|
+
* @param guards Guards to evaluate; passes if any guard passes.
|
|
65
|
+
* @returns Guard for the union of all guarded types.
|
|
66
|
+
*/
|
|
67
|
+
declare function or<P extends readonly Guard<unknown>[]>(...guards: P): Guard<OutOfGuards<P>>;
|
|
68
|
+
/**
|
|
69
|
+
* Adapts a guard so it can be used as a refinement within a known supertype.
|
|
70
|
+
*
|
|
71
|
+
* @returns Function that converts a `Guard<T>` into a `Refine<A, T>` when `T extends A`.
|
|
72
|
+
*/
|
|
73
|
+
declare function guardIn<A>(): <T extends A>(guard: Guard<T>) => Refine<A, T>;
|
|
74
|
+
/**
|
|
75
|
+
* Logical negation of a guard/refinement.
|
|
76
|
+
*
|
|
77
|
+
* @param guard Guard or refinement to negate.
|
|
78
|
+
* @returns Refinement excluding the guarded subtype from the input type.
|
|
79
|
+
*/
|
|
80
|
+
declare function not<A, T>(guard: Guard<T>): Refine<A, Exclude<A, Extract<A, T>>>;
|
|
81
|
+
declare function not<A, B extends A>(refine: Refine<A, B>): Refine<A, Exclude<A, B>>;
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Allows `null` in addition to values accepted by the given guard/refinement.
|
|
85
|
+
*
|
|
86
|
+
* @param guard Guard/refinement for the base type.
|
|
87
|
+
* @returns Guard/refinement widened to include `null`.
|
|
88
|
+
*/
|
|
89
|
+
declare function nullable<T>(guard: Guard<T>): Guard<T | null>;
|
|
90
|
+
declare function nullable<A, B extends A>(refine: Refine<A, B>): Refine<A | null, B | null>;
|
|
91
|
+
/**
|
|
92
|
+
* Excludes `null` from the accepted values.
|
|
93
|
+
*
|
|
94
|
+
* @param guard Guard/refinement possibly including `null`.
|
|
95
|
+
* @returns Guard/refinement with `null` excluded.
|
|
96
|
+
*/
|
|
97
|
+
declare function nonNull<T>(guard: Guard<T>): Guard<Exclude<T, null>>;
|
|
98
|
+
declare function nonNull<A, B extends A>(refine: Refine<A, B>): Refine<Exclude<A, null>, Exclude<B, null>>;
|
|
99
|
+
/**
|
|
100
|
+
* Allows `null` or `undefined` in addition to values accepted by the guard.
|
|
101
|
+
*
|
|
102
|
+
* @param guard Guard/refinement for the base type.
|
|
103
|
+
* @returns Guard/refinement widened to include `null | undefined`.
|
|
104
|
+
*/
|
|
105
|
+
declare function nullish<T>(guard: Guard<T>): Guard<T | null | undefined>;
|
|
106
|
+
declare function nullish<A, B extends A>(refine: Refine<A, B>): Refine<A | null | undefined, B | null | undefined>;
|
|
107
|
+
/**
|
|
108
|
+
* Allows `undefined` in addition to values accepted by the guard.
|
|
109
|
+
*
|
|
110
|
+
* @param guard Guard/refinement for the base type.
|
|
111
|
+
* @returns Guard/refinement widened to include `undefined`.
|
|
112
|
+
*/
|
|
113
|
+
declare function optional<T>(guard: Guard<T>): Guard<T | undefined>;
|
|
114
|
+
declare function optional<A, B extends A>(refine: Refine<A, B>): Refine<A | undefined, B | undefined>;
|
|
115
|
+
/**
|
|
116
|
+
* Requires a value to be present (excludes `undefined`).
|
|
117
|
+
*
|
|
118
|
+
* @param guard Guard/refinement that may accept `undefined`.
|
|
119
|
+
* @returns Guard/refinement with `undefined` removed.
|
|
120
|
+
*/
|
|
121
|
+
declare function required<T>(guard: Guard<T | undefined>): Guard<T>;
|
|
122
|
+
declare function required<A, B extends A>(refine: Refine<A | undefined, B | undefined>): Refine<A, B>;
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Creates a guard that matches values equal to the provided target using `Object.is` semantics.
|
|
126
|
+
*
|
|
127
|
+
* @param target Target value to compare against.
|
|
128
|
+
* @returns Predicate narrowing to the exact literal type of `target`.
|
|
129
|
+
*/
|
|
130
|
+
declare function equals<const T>(target: T): Predicate<T>;
|
|
131
|
+
/**
|
|
132
|
+
* Creates a comparator builder based on a guard and selector.
|
|
133
|
+
* Useful for checking whether a selected key or projection equals a target value.
|
|
134
|
+
*
|
|
135
|
+
* @param guard Guard for the base type before selecting a field.
|
|
136
|
+
* @param selector Optional selector to project the comparable key; if omitted, returns a function to supply it later.
|
|
137
|
+
* @returns Function that accepts a target and returns a predicate over the original value.
|
|
138
|
+
*/
|
|
139
|
+
declare function equalsBy<F extends (value: unknown) => value is unknown>(guard: F): <K>(selector: (value: GuardedOf<F>) => K) => <const T extends K>(target: T) => Predicate<GuardedOf<F>>;
|
|
140
|
+
declare function equalsBy<F extends (value: unknown) => value is unknown, K>(guard: F, selector: (value: GuardedOf<F>) => K): <const T extends K>(target: T) => Predicate<GuardedOf<F>>;
|
|
141
|
+
/**
|
|
142
|
+
* Creates a guard that matches objects having a key equal to the target value.
|
|
143
|
+
*
|
|
144
|
+
* @param key Property key to compare.
|
|
145
|
+
* @param target Value to compare using `Object.is`.
|
|
146
|
+
* @returns Predicate narrowing to objects where `key` exists and equals `target`.
|
|
147
|
+
*/
|
|
148
|
+
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>;
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Executes a guard or refinement and returns a tagged result with the value when valid.
|
|
152
|
+
*
|
|
153
|
+
* @param guard Guard/refinement to evaluate.
|
|
154
|
+
* @param value Value to check.
|
|
155
|
+
* @returns `{ valid: true, value }` when the guard passes; `{ valid: false }` otherwise.
|
|
156
|
+
*/
|
|
157
|
+
declare function safeParse<T>(guard: Guard<T>, value: unknown): ParseResult<T>;
|
|
158
|
+
declare function safeParse<A, B extends A>(refine: Refine<A, B>, value: A): ParseResult<B>;
|
|
159
|
+
declare function safeParse(fn: (value: unknown) => boolean, value: unknown): ParseResult<unknown>;
|
|
160
|
+
/**
|
|
161
|
+
* Curried variant of `safeParse` for reuse.
|
|
162
|
+
*
|
|
163
|
+
* @param guard Guard/refinement to evaluate.
|
|
164
|
+
* @returns Function that takes a value and returns the `ParseResult`.
|
|
165
|
+
*/
|
|
166
|
+
declare function safeParseWith<T>(guard: Guard<T>): (value: unknown) => ParseResult<T>;
|
|
167
|
+
declare function safeParseWith<A, B extends A>(refine: Refine<A, B>): (value: A) => ParseResult<B>;
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Checks whether a value is a string.
|
|
171
|
+
*
|
|
172
|
+
* @returns Predicate narrowing to `string`.
|
|
173
|
+
*/
|
|
174
|
+
declare const isString: Predicate<string>;
|
|
175
|
+
/**
|
|
176
|
+
* Checks whether a value is a number primitive (may include NaN/±Infinity).
|
|
177
|
+
*
|
|
178
|
+
* @returns Predicate narrowing to `number` (primitive-only).
|
|
179
|
+
*/
|
|
180
|
+
declare const isNumberPrimitive: Predicate<number>;
|
|
181
|
+
/**
|
|
182
|
+
* Checks whether a number is finite.
|
|
183
|
+
* @returns Refinement that accepts only finite numbers.
|
|
184
|
+
*/
|
|
185
|
+
declare const isFiniteNumber: Refine<number, number>;
|
|
186
|
+
/**
|
|
187
|
+
* Checks whether a value is a finite number.
|
|
188
|
+
* @returns Predicate narrowing to finite `number`.
|
|
189
|
+
*/
|
|
190
|
+
declare const isNumber: Predicate<number>;
|
|
191
|
+
/**
|
|
192
|
+
* Checks whether a value is a boolean.
|
|
193
|
+
*
|
|
194
|
+
* @returns Predicate narrowing to `boolean`.
|
|
195
|
+
*/
|
|
196
|
+
declare const isBoolean: Predicate<boolean>;
|
|
197
|
+
/**
|
|
198
|
+
* Checks whether a value is a bigint.
|
|
199
|
+
*
|
|
200
|
+
* @returns Predicate narrowing to `bigint`.
|
|
201
|
+
*/
|
|
202
|
+
declare const isBigInt: Predicate<bigint>;
|
|
203
|
+
/**
|
|
204
|
+
* Checks whether a value is a symbol.
|
|
205
|
+
*
|
|
206
|
+
* @returns Predicate narrowing to `symbol`.
|
|
207
|
+
*/
|
|
208
|
+
declare const isSymbol: Predicate<symbol>;
|
|
209
|
+
/**
|
|
210
|
+
* Checks whether a value is exactly `undefined`.
|
|
211
|
+
*
|
|
212
|
+
* @returns Predicate narrowing to `undefined`.
|
|
213
|
+
*/
|
|
214
|
+
declare const isUndefined: Predicate<undefined>;
|
|
215
|
+
/**
|
|
216
|
+
* Checks whether a value is exactly `null`.
|
|
217
|
+
*
|
|
218
|
+
* @returns Predicate narrowing to `null`.
|
|
219
|
+
*/
|
|
220
|
+
declare const isNull: Predicate<null>;
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* Validates an array where every element satisfies the provided guard.
|
|
224
|
+
*
|
|
225
|
+
* @param elementGuard Guard applied to each array element.
|
|
226
|
+
* @returns Predicate narrowing to a readonly array of the guarded element type.
|
|
227
|
+
*/
|
|
228
|
+
declare function arrayOf<F extends (value: unknown) => value is unknown>(elementGuard: F): Predicate<readonly GuardedOf<F>[]>;
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* Validates a fixed-length tuple by applying element-wise guards.
|
|
232
|
+
*
|
|
233
|
+
* @param guards Guards corresponding to each tuple position.
|
|
234
|
+
* @returns Predicate that narrows to a readonly tuple of guarded element types.
|
|
235
|
+
*/
|
|
236
|
+
declare function tupleOf<const Fs extends readonly Predicate<unknown>[]>(...guards: Fs): Predicate<{
|
|
237
|
+
readonly [K in keyof Fs]: GuardedOf<Fs[K]>;
|
|
238
|
+
}>;
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* Combines multiple guards; passes when any one guard matches.
|
|
242
|
+
*
|
|
243
|
+
* @param guards One or more type guards/refinements to try.
|
|
244
|
+
* @returns Predicate that narrows to the union of guarded types.
|
|
245
|
+
*/
|
|
246
|
+
declare function oneOf<Fs extends readonly ((value: unknown) => value is unknown)[]>(...guards: Fs): Predicate<GuardedOf<Fs[number]>>;
|
|
247
|
+
declare function oneOf<A, Fs extends readonly ((value: A) => value is A)[]>(...guards: Fs): (input: A) => input is GuardedWithin<Fs, A>;
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* Validates a record-like object by guarding both keys and values.
|
|
251
|
+
*
|
|
252
|
+
* @param keyFunction Guard for string keys (e.g., specific literal keys pattern).
|
|
253
|
+
* @param valueFunction Guard applied to each value in the record.
|
|
254
|
+
* @returns Predicate narrowing to a readonly record with guarded key/value types.
|
|
255
|
+
*/
|
|
256
|
+
declare function recordOf<KF extends (value: unknown) => value is string, VF extends (value: unknown) => value is unknown>(keyFunction: KF, valueFunction: VF): Predicate<Readonly<Record<GuardedOf<KF>, GuardedOf<VF>>>>;
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* Validates an object against a field-to-guard schema.
|
|
260
|
+
* Keys in the schema are required; optionally rejects extra keys when `exact: true`.
|
|
261
|
+
*
|
|
262
|
+
* @param schema Record of property guards.
|
|
263
|
+
* @param options When `{ exact: true }`, disallows properties not in `schema`.
|
|
264
|
+
* @returns Predicate that narrows to the inferred struct type.
|
|
265
|
+
*/
|
|
266
|
+
declare function struct<S extends Schema>(schema: S, options?: {
|
|
267
|
+
exact?: boolean;
|
|
268
|
+
}): Predicate<InferSchema<S>>;
|
|
269
|
+
|
|
270
|
+
/**
|
|
271
|
+
* Creates a guard that matches when the input is one of the provided literal values.
|
|
272
|
+
* Uses `Object.is` for exact value semantics.
|
|
273
|
+
*
|
|
274
|
+
* @param values Literal primitives to compare against.
|
|
275
|
+
* @returns Predicate narrowing to the union of the provided literal values.
|
|
276
|
+
*/
|
|
277
|
+
declare function oneOfValues<const T extends ReadonlyArray<Primitive>>(...values: T): Predicate<T[number]>;
|
|
278
|
+
|
|
279
|
+
/**
|
|
280
|
+
* Checks whether a value is a function.
|
|
281
|
+
*
|
|
282
|
+
* @returns Predicate narrowing to `Function`.
|
|
283
|
+
*/
|
|
284
|
+
declare const isFunction: Predicate<Function>;
|
|
285
|
+
/**
|
|
286
|
+
* Checks for non-null objects (including arrays, dates, etc.).
|
|
287
|
+
*
|
|
288
|
+
* @returns Predicate narrowing to `Record<PropertyKey, unknown>`.
|
|
289
|
+
*/
|
|
290
|
+
declare const isObject: Predicate<Record<PropertyKey, unknown>>;
|
|
291
|
+
/**
|
|
292
|
+
* Checks for plain objects created by object literals or `Object.create(null)`.
|
|
293
|
+
*
|
|
294
|
+
* @returns Predicate narrowing to `Record<string, unknown>`.
|
|
295
|
+
*/
|
|
296
|
+
declare const isPlainObject: Predicate<Record<string, unknown>>;
|
|
297
|
+
/**
|
|
298
|
+
* Checks whether a value is an array.
|
|
299
|
+
*
|
|
300
|
+
* @returns Predicate narrowing to `readonly unknown[]`.
|
|
301
|
+
*/
|
|
302
|
+
declare const isArray: Predicate<readonly unknown[]>;
|
|
303
|
+
/**
|
|
304
|
+
* Checks whether a value is a valid `Date` instance (not `Invalid Date`).
|
|
305
|
+
*
|
|
306
|
+
* @returns Predicate narrowing to `Date`.
|
|
307
|
+
*/
|
|
308
|
+
declare const isDate: Predicate<Date>;
|
|
309
|
+
/**
|
|
310
|
+
* Checks whether a value is a `RegExp`.
|
|
311
|
+
*
|
|
312
|
+
* @returns Predicate narrowing to `RegExp`.
|
|
313
|
+
*/
|
|
314
|
+
declare const isRegExp: Predicate<RegExp>;
|
|
315
|
+
/**
|
|
316
|
+
* Checks whether a value is a `Map`.
|
|
317
|
+
*
|
|
318
|
+
* @returns Predicate narrowing to `Map<unknown, unknown>`.
|
|
319
|
+
*/
|
|
320
|
+
declare const isMap: Predicate<Map<unknown, unknown>>;
|
|
321
|
+
/**
|
|
322
|
+
* Checks whether a value is a `Set`.
|
|
323
|
+
*
|
|
324
|
+
* @returns Predicate narrowing to `Set<unknown>`.
|
|
325
|
+
*/
|
|
326
|
+
declare const isSet: Predicate<Set<unknown>>;
|
|
327
|
+
/**
|
|
328
|
+
* Checks whether a value is promise-like (has a `then` function).
|
|
329
|
+
*
|
|
330
|
+
* @returns Predicate narrowing to `PromiseLike<unknown>`.
|
|
331
|
+
*/
|
|
332
|
+
declare const isPromiseLike: Predicate<PromiseLike<unknown>>;
|
|
333
|
+
/**
|
|
334
|
+
* Checks whether a value implements the `Iterable` protocol.
|
|
335
|
+
*
|
|
336
|
+
* @returns Predicate narrowing to `Iterable<unknown>`.
|
|
337
|
+
*/
|
|
338
|
+
declare const isIterable: Predicate<Iterable<unknown>>;
|
|
339
|
+
/**
|
|
340
|
+
* Checks whether a value implements the `AsyncIterable` protocol.
|
|
341
|
+
*
|
|
342
|
+
* @returns Predicate narrowing to `AsyncIterable<unknown>`.
|
|
343
|
+
*/
|
|
344
|
+
declare const isAsyncIterable: Predicate<AsyncIterable<unknown>>;
|
|
345
|
+
/**
|
|
346
|
+
* Checks whether a value is an `ArrayBuffer`.
|
|
347
|
+
*
|
|
348
|
+
* @returns Predicate narrowing to `ArrayBuffer`.
|
|
349
|
+
*/
|
|
350
|
+
declare const isArrayBuffer: Predicate<ArrayBuffer>;
|
|
351
|
+
/**
|
|
352
|
+
* Checks whether a value is a `DataView`.
|
|
353
|
+
*
|
|
354
|
+
* @returns Predicate narrowing to `DataView`.
|
|
355
|
+
*/
|
|
356
|
+
declare const isDataView: Predicate<DataView<ArrayBufferLike>>;
|
|
357
|
+
/**
|
|
358
|
+
* Checks whether a value is a typed array view (any `ArrayBufferView` except DataView).
|
|
359
|
+
*
|
|
360
|
+
* @returns Predicate narrowing to `ArrayBufferView`.
|
|
361
|
+
*/
|
|
362
|
+
declare const isTypedArray: Predicate<ArrayBufferView<ArrayBufferLike>>;
|
|
363
|
+
/**
|
|
364
|
+
* Checks whether a value is an `Error` (instance of Error or plain Error object tag).
|
|
365
|
+
*
|
|
366
|
+
* @returns Predicate narrowing to `Error`.
|
|
367
|
+
*/
|
|
368
|
+
declare const isError: Predicate<Error>;
|
|
369
|
+
/**
|
|
370
|
+
* Checks whether a value is a `URL` (in environments with URL available).
|
|
371
|
+
*
|
|
372
|
+
* @returns Predicate narrowing to `URL` when supported; otherwise always false.
|
|
373
|
+
*/
|
|
374
|
+
declare const isURL: Predicate<URL>;
|
|
375
|
+
/**
|
|
376
|
+
* Checks whether a value is a `Blob` (in environments with Blob available).
|
|
377
|
+
*
|
|
378
|
+
* @returns Predicate narrowing to `Blob` when supported; otherwise always false.
|
|
379
|
+
*/
|
|
380
|
+
declare const isBlob: Predicate<Blob>;
|
|
381
|
+
|
|
382
|
+
/**
|
|
383
|
+
* Converts an array of guards to plain boolean predicates for iteration helpers.
|
|
384
|
+
*
|
|
385
|
+
* @param guards Guards to be treated as simple predicates.
|
|
386
|
+
* @returns Readonly array of boolean-returning functions.
|
|
387
|
+
*/
|
|
388
|
+
declare const toBooleanPredicates: (guards: readonly Guard<unknown>[]) => ReadonlyArray<(value: unknown) => boolean>;
|
|
389
|
+
|
|
390
|
+
export { type ChainResult, type Guard, type GuardedOf, type GuardedWithin, type InferSchema, type OutOfGuards, type ParseResult, type Predicate, type Primitive, type Refine, type RefineChain, type Refinement, type Schema, and, andAll, arrayOf, define, equals, equalsBy, equalsKey, guardIn, isArray, isArrayBuffer, isAsyncIterable, isBigInt, isBlob, isBoolean, isDataView, isDate, isError, isFiniteNumber, isFunction, isIterable, isMap, isNull, isNumber, isNumberPrimitive, isObject, isPlainObject, isPromiseLike, isRegExp, isSet, isString, isSymbol, isTypedArray, isURL, isUndefined, nonNull, not, nullable, nullish, oneOf, oneOfValues, optional, or, predicateToRefine, recordOf, required, safeParse, safeParseWith, struct, toBooleanPredicates, tupleOf };
|