Skip to main content

latest
It is unknown whether this package works with Cloudflare Workers, Node.js, Deno, Bun, Browsers
It is unknown whether this package works with Cloudflare Workers
It is unknown whether this package works with Node.js
It is unknown whether this package works with Deno
It is unknown whether this package works with Bun
It is unknown whether this package works with Browsers
JSR Score
47%
Published
3 months ago (0.1.0)
Package root>index.ts
export const isNonNull = <T>(value: T): value is NonNullable<T> => value !== null && value !== undefined; export const isObject = (value: unknown): value is object => typeof value === 'object' && value !== null; export const isFunction = (value: unknown): value is Function => typeof value === 'function'; export const isString = (value: unknown): value is string => typeof value === 'string'; export const isNumber = (value: unknown): value is number => typeof value === 'number'; export const isArrayOf = <T>( value: unknown, predicate: (value: unknown) => value is T ): value is T[] => Array.isArray(value) && value.every(predicate); export const isObjectOf = <T, Original extends object>( value: Original, predicate: (value: unknown) => value is T ): value is Original & { [key: string]: T } => isObject(value) && Object.values(value).every(predicate); export const isArrayElement = <T>( value: unknown, array: readonly T[] ): value is T => array.includes(value as T); export const isOptional = <T>( value: unknown, predicate: (v: unknown) => v is T ): value is T | undefined => value === undefined || predicate(value); export const hasProperty = <T extends object, K extends string, V>( value: T, key: K, predicate: (value: unknown) => value is V ): value is T & { [key in K]: V } => key in value && predicate((value as { [key in K]: V })[key]); export function assert<T>( value: unknown, predicate: (v: unknown) => v is T ): asserts value is T { if (!predicate(value)) throw new Error('Invalid value'); } export function assertHasProperty<T extends object, K extends string, V>( value: T, key: K, predicate: (v: unknown) => v is V ): asserts value is T & { [key in K]: V } { if (!hasProperty(value, key, predicate)) throw new Error(`Invalid value for property key ${String(key)}`); }