is-kit

key

Helpers for key-based guards and literal narrowing. Use narrowKeyTo to build reusable guards that constrain a specific property to a literal value.

import { narrowKeyTo, or, struct, isString, isNumber, oneOfValues } from 'is-kit';
type User = { id: string; age: number; role: 'admin' | 'guest' | 'trial' };
const isUser = struct({
id: isString,
age: isNumber,
role: oneOfValues('admin', 'guest', 'trial'),
});
// Build role-specific guards that also narrow the 'role' field to literals
const byRole = narrowKeyTo(isUser, 'role');
const isAdmin = byRole('admin'); // Readonly<User> & { role: 'admin' }
const isGuest = byRole('guest'); // Readonly<User> & { role: 'guest' }
const isTrial = byRole('trial'); // Readonly<User> & { role: 'trial' }
// Compose as usual
const isGuestOrTrial = or(isGuest, isTrial);
declare const input: unknown;
if (isGuestOrTrial(input)) {
// input.role is narrowed to 'guest' | 'trial'
}