API reference › @evolu/common › Types › RefinementWithIndex
type RefinementWithIndex<A, B> = (a: A, index: number) => a is B;
Defined in: packages/common/src/Types.ts:160
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.
Indexed refinement
import { partitionArray, type RefinementWithIndex } from "@evolu/common";
type Item = {
readonly type: "number" | "string";
readonly value: unknown;
};
type NumberItem = Item & { readonly type: "number" };
const isNumberItem: RefinementWithIndex<Item, NumberItem> = (
item,
index,
): item is NumberItem => index > 0 && item.type === "number";
const items: ReadonlyArray<Item> = [
{ type: "number", value: 1 },
{ type: "number", value: 2 },
];
const [numbers, others] = partitionArray(items, isNumberItem);
expectTypeOf(numbers).toEqualTypeOf<ReadonlyArray<NumberItem>>();
expect(numbers[0]?.value).toBe(2);
expect(others[0]?.value).toBe(1);