is-kit 1.14.1 → 1.14.3
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 +38 -1
- package/dist/index.d.mts +193 -208
- package/dist/index.d.ts +193 -208
- package/dist/index.js +811 -607
- package/dist/index.mjs +729 -499
- package/package.json +4 -3
package/README.md
CHANGED
|
@@ -135,6 +135,23 @@ This is the core idea of `is-kit`:
|
|
|
135
135
|
2. Compose them.
|
|
136
136
|
3. Reuse them anywhere TypeScript narrowing matters.
|
|
137
137
|
|
|
138
|
+
## ESLint integration
|
|
139
|
+
|
|
140
|
+
[`eslint-plugin-is-kit`](https://github.com/nyaomaru/eslint-plugin-is-kit)
|
|
141
|
+
detects ambiguous or redundant array predicates and can suggest reusable
|
|
142
|
+
is-kit guards when they preserve runtime behavior and useful narrowing.
|
|
143
|
+
|
|
144
|
+
```ts
|
|
145
|
+
values.filter(Boolean); // May also remove "", 0, false, and NaN.
|
|
146
|
+
values.filter(isNotNil); // Removes only null and undefined.
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
```sh
|
|
150
|
+
pnpm add -D eslint-plugin-is-kit
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
See the [plugin setup and rule reference](https://github.com/nyaomaru/eslint-plugin-is-kit#readme).
|
|
154
|
+
|
|
138
155
|
## 🧭 Guard Composition Guide
|
|
139
156
|
|
|
140
157
|
When writing reusable guards with `is-kit`, start from the library primitives:
|
|
@@ -375,11 +392,25 @@ needs additional narrowing.
|
|
|
375
392
|
```ts
|
|
376
393
|
import { isString, refineDefinedKey, refineIndex, refineKey } from 'is-kit';
|
|
377
394
|
|
|
395
|
+
type Item = {
|
|
396
|
+
readonly value: string | number;
|
|
397
|
+
readonly label?: string | number;
|
|
398
|
+
};
|
|
399
|
+
|
|
378
400
|
const hasStringValue = refineKey('value', isString);
|
|
379
401
|
const hasDefinedStringLabel = refineDefinedKey('label', isString);
|
|
380
402
|
const hasStringAtZero = refineIndex(0, isString);
|
|
403
|
+
|
|
404
|
+
declare const items: readonly Item[];
|
|
405
|
+
const textItems = items.filter(hasStringValue);
|
|
406
|
+
// Array<Item & Record<'value', string>>
|
|
381
407
|
```
|
|
382
408
|
|
|
409
|
+
The important part is reusable parent narrowing: the predicate retains both
|
|
410
|
+
the original `Item` type and the checked property type through `filter`,
|
|
411
|
+
`find`, and control-flow branches without a handwritten intersection
|
|
412
|
+
annotation.
|
|
413
|
+
|
|
383
414
|
- `refineKey` refines one required property using normal property access.
|
|
384
415
|
- `refineDefinedKey` returns `false` for a missing or `undefined` property.
|
|
385
416
|
- `refineIndex` returns `false` for an out-of-bounds, sparse, or `undefined`
|
|
@@ -392,6 +423,11 @@ numeric property.
|
|
|
392
423
|
Each key or index must identify one concrete runtime location. This keeps one
|
|
393
424
|
successful lookup from incorrectly narrowing multiple properties.
|
|
394
425
|
|
|
426
|
+
See [Refine properties on existing TypeScript
|
|
427
|
+
types](https://is-kit.dev/guides/refine-properties-on-existing-types) for
|
|
428
|
+
required, optional, indexed, nested, and literal examples. The TypeScript
|
|
429
|
+
Compiler API is one advanced application of this generic pattern.
|
|
430
|
+
|
|
395
431
|
### 8. Handle null and undefined explicitly
|
|
396
432
|
|
|
397
433
|
Use the nullish helpers to say exactly what is allowed.
|
|
@@ -642,7 +678,8 @@ For detailed API pages and more examples, see:
|
|
|
642
678
|
|
|
643
679
|
- [Practical guides](https://is-kit.dev/guides)
|
|
644
680
|
- [Keep hand-written type guards in sync with TypeScript types](https://is-kit.dev/guides/keep-type-guards-in-sync)
|
|
645
|
-
- [
|
|
681
|
+
- [Refine properties on existing TypeScript types](https://is-kit.dev/guides/refine-properties-on-existing-types)
|
|
682
|
+
- [Advanced property refinement with the TypeScript Compiler API](https://is-kit.dev/guides/typescript-compiler-api)
|
|
646
683
|
- [Validate unknown without a schema library](https://is-kit.dev/guides/validate-unknown-without-schema-library)
|
|
647
684
|
- [API reference](https://is-kit.dev/api-reference)
|
|
648
685
|
- [Documentation home](https://is-kit.dev/)
|