@klyper/types
v0.3.0
Published
Klyper types package.
Downloads
27
Readme
@klyper/types
Shared TypeScript type aliases and runtime type guards for Klyper packages.
Use this package when a module needs small, dependency-free helpers for narrowing unknown values or reusing common utility types.
Installation
npm install @klyper/typesThis package is published as an ES module package and also exposes a CommonJS
entry through main.
Usage
import { isPlainObject, isString, type } from "@klyper/types";
const value: unknown = { title: "Klyper" };
if (isPlainObject(value) && isString(value.title)) {
value.title.toUpperCase();
}
type(value); // "object"The runtime helpers accept unknown values and return TypeScript type predicates
where possible.
Runtime API
Type detection
| Export | Description |
| --- | --- |
| type(value) | Returns a normalized logical type name. null and undefined both return "null". |
Recognized logical type names include "array", "asyncfunction", "bigint",
"boolean", "date", "error", "function", "map", "null", "number",
"object", "promise", "regexp", "set", "string", "symbol",
"weakmap", and "weakset".
Type guards
| Export | Narrows to | Notes |
| --- | --- | --- |
| isArray(value) | Array<any> | Checks array values. |
| isAsyncFunction(value) | TypeAnyAsyncFunction | Checks functions declared with async. |
| isBigInt(value) | bigint | Checks bigint primitives. |
| isBoolean(value) | boolean | Checks boolean primitives. |
| isConstructor(value) | TypeAnyConstructor | Checks functions whose prototype constructor points back to the function. |
| isDate(value) | Date | Checks Date instances. |
| isDef<T>(value) | T | Returns false for null and undefined. |
| isError(value) | Error | Checks Error instances. |
| isFiniteNumber(value) | number | Checks finite number primitives. |
| isFloat(value) | number | Checks finite numbers with a fractional part. |
| isFunction(value) | TypeAnyFunction | Checks callable values. |
| isInteger(value) | number | Checks integer number primitives. |
| isMap<K, V>(value) | Map<K, V> | Checks Map instances. |
| isNil(value) | null \| undefined | Checks only null and undefined. |
| isNonEmptyArray<T>(value) | [T, ...Array<T>] | Checks arrays with at least one item. |
| isNull(value) | null | Checks only null. |
| isNumber(value) | number | Checks number primitives, including NaN and infinities. |
| isNumberLike(value) | boolean | Checks strings that represent finite decimal numbers. Scientific, binary, hex, NaN, and Infinity strings are rejected. |
| isObject(value) | TypeAnyObject | Checks values whose normalized logical type is "object", including object literals, Object.create(null), and class instances. Arrays, maps, sets, dates, and regexps are excluded. |
| isPlainObject(value) | TypeAnyObject | Checks object literals and Object.create(null) values. Class instances are excluded. |
| isPromise<T>(value) | Promise<T> | Checks Promise instances, not arbitrary thenables. |
| isRegExp(value) | RegExp | Checks regular expressions. |
| isScalar(value) | TypeScalar | Checks boolean, number, and string values. |
| isSet<T>(value) | Set<T> | Checks Set instances. |
| isString(value) | string | Checks string primitives. |
| isSymbol(value) | symbol | Checks symbol primitives. |
| isUndefined(value) | undefined | Checks only undefined. |
Type Aliases
Common exported type aliases include:
import type {
TypeAnyAsyncFunction,
TypeAnyConstructor,
TypeAnyFunction,
TypeAnyObject,
TypeCallback,
TypeNullable,
TypeName,
TypeObjectOptional,
TypeObjectRequired,
TypeScalar,
} from "@klyper/types";Notable groups:
- Array aliases:
TypeArray,TypeAnyArray,TypeFunctionArray,TypeNumberArray,TypeObjectArray, andTypeStringArray. - Object aliases:
TypeObject,TypeAnyObject,TypeBooleanObject,TypeFunctionObject,TypeNumberObject, andTypeStringObject. - Object shape helpers:
TypeObjectOptional,TypeObjectRequired,TypeObjectKeys, andTypeObjectKeysFunction. - Callback aliases:
TypeCallback,TypeCallbackArray,TypeCallbackMap, andTypeCallbackObject. - Value helpers:
TypeKey,TypeNullable,TypeOptional, andTypeScalar.
Primitive guards such as isString, isNumber, isBoolean, isBigInt, and
isSymbol only accept primitives. Boxed wrappers such as new String("x"),
new Number(1), and new Boolean(false) return false.
Commands
npm run build --workspace @klyper/types
npm run check --workspace @klyper/types
npm run lint --workspace @klyper/types
npm test --workspace @klyper/typesbuild uses Rollup for the package output. check runs TypeScript without emitting files.
Validation
Run the package checks before publishing or changing exports:
npm run check --workspace @klyper/types
npm test --workspace @klyper/types