is-kit

key

Helpers for key-based guards and literal narrowing. Use hasKey to check for a required own property and narrowKeyTo to constrain a property to a literal value.

hasKey

Narrow an unknown value to an object that owns a specific key. Handy before custom refinements or discriminated-union checks.

import { hasKey } from 'is-kit';
const hasKind = hasKey('kind');
declare const input: unknown;
if (hasKind(input)) {
// input: Record<'kind', unknown>
input.kind;
}

narrowKeyTo

Build reusable guards that narrow a property to specific literal values.

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'
}