API reference / @evolu/common / Types / RefinementWithIndex
Type Alias: RefinementWithIndex()<A, B>
type RefinementWithIndex<A, B> = (a, index) => a is B;
Defined in: packages/common/src/Types.ts:82
A type guard function that refines type A to a narrower type B at a given
index.
Useful for callbacks that need both the element and its position while maintaining type narrowing.
Example
type Item = { type: "number" | "string"; value: unknown };
const isNumberItem: RefinementWithIndex<Item, Item & { type: "number" }> =
(item, index): item is Item & { type: "number" } =>
index > 0 && item.type === "number";
const items: ReadonlyArray<Item> = [...];
const [numbers, others] = partitionArray(items, isNumberItem);
Type Parameters
| Type Parameter |
|---|
A |
B extends A |
Parameters
| Parameter | Type |
|---|---|
a | A |
index | number |
Returns
a is B