is-type-check-util
v1.0.4
Published
Lightweight, tree-shakeable type-checking utilities for JavaScript and TypeScript
Maintainers
Readme
is-type-check-util
Lightweight, tree-shakeable type-checking utilities for JavaScript and TypeScript. Zero dependencies. Full TypeScript type narrowing support.
Why is-type-check-util?
- Tiny — 5 KB packed, tree-shake down to bytes
- Zero dependencies — nothing extra to install
- Dual API — unified
isType(val, 'string')+ standaloneisString(val)helpers - TypeScript-first — every helper is a type predicate for automatic narrowing
- 30+ type checks — primitives, objects, collections, functions, buffers, and more
- Dual format — ships both ESM and CommonJS
- Case-insensitive —
isType([], 'Array')just works
Installation
npm install is-type-check-utilyarn add is-type-check-utilpnpm add is-type-check-utilUsage
Unified isType() API
A single function that accepts any value and a type name string:
import { isType } from 'is-type-check-util';
isType('hello', 'string'); // true
isType(42, 'number'); // true
isType([], 'array'); // true
isType(null, 'null'); // true
isType(new Date(), 'date'); // true
isType(new Map(), 'map'); // true
isType(async () => {}, 'asyncfunction'); // true
isType({}, 'plainobject'); // true
// Case-insensitive
isType([], 'Array'); // true
isType(new Set(), 'SET'); // true
// Throws on unknown types
isType('hello', 'foo'); // Error: Unknown type: "foo"Standalone helpers
Import only what you need — each function is independently tree-shakeable:
import { isString, isArray, isPlainObject } from 'is-type-check-util';
isString('hello'); // true
isString(42); // false
isArray([1, 2, 3]); // true
isArray('not array'); // false
isPlainObject({}); // true
isPlainObject(new Date()); // false
isPlainObject(Object.create(null)); // truePrimitives
import { isNumber, isBoolean, isNull, isUndefined, isSymbol, isBigInt } from 'is-type-check-util';
isNumber(42); // true
isNumber(NaN); // true (NaN is technically a number)
isBoolean(false); // true
isNull(null); // true
isUndefined(undefined); // true
isSymbol(Symbol('id')); // true
isBigInt(42n); // trueObjects and collections
import { isDate, isRegExp, isMap, isSet, isWeakMap, isWeakSet, isObject } from 'is-type-check-util';
isDate(new Date()); // true
isRegExp(/abc/); // true
isMap(new Map()); // true
isSet(new Set()); // true
isWeakMap(new WeakMap()); // true
isWeakSet(new WeakSet()); // true
isObject({}); // true (any non-null object)
isObject(null); // falseFunctions
import { isFunction, isAsyncFunction, isGeneratorFunction } from 'is-type-check-util';
isFunction(() => {}); // true
isAsyncFunction(async () => {}); // true
isGeneratorFunction(function* () {}); // trueErrors, buffers, and more
import { isError, isBuffer, isArrayBuffer, isTypedArray, isPromise } from 'is-type-check-util';
isError(new TypeError('oops')); // true
isBuffer(Buffer.from('data')); // true
isArrayBuffer(new ArrayBuffer(8)); // true
isTypedArray(new Uint8Array(4)); // true
isPromise(Promise.resolve()); // true
isPromise({ then: () => {} }); // true (thenable)Utility checks
import { isNil, isNaN, isFinite, isInteger, isArguments } from 'is-type-check-util';
isNil(null); // true
isNil(undefined); // true
isNil(0); // false
isNaN(NaN); // true
isNaN('hello'); // false (stricter than global isNaN)
isFinite(42); // true
isFinite(Infinity); // false
isInteger(10); // true
isInteger(10.5); // falseTypeScript type narrowing
All standalone helpers are type predicates, so TypeScript narrows automatically:
import { isString, isArray } from 'is-type-check-util';
function process(input: unknown) {
if (isString(input)) {
console.log(input.toUpperCase()); // input is string
}
if (isArray(input)) {
console.log(input.length); // input is unknown[]
}
}Supported types
| Type name | Checker function | Category |
|---|---|---|
| string | isString | Primitive |
| number | isNumber | Primitive |
| boolean | isBoolean | Primitive |
| null | isNull | Primitive |
| undefined | isUndefined | Primitive |
| symbol | isSymbol | Primitive |
| bigint | isBigInt | Primitive |
| array | isArray | Object |
| object | isObject | Object |
| plainobject | isPlainObject | Object |
| date | isDate | Object |
| regexp | isRegExp | Object |
| map | isMap | Collection |
| set | isSet | Collection |
| weakmap | isWeakMap | Collection |
| weakset | isWeakSet | Collection |
| function | isFunction | Function |
| asyncfunction | isAsyncFunction | Function |
| generatorfunction | isGeneratorFunction | Function |
| error | isError | Error |
| buffer | isBuffer | Buffer |
| arraybuffer | isArrayBuffer | Buffer |
| typedarray | isTypedArray | Buffer |
| promise | isPromise | Utility |
| nan | isNaN | Utility |
| finite | isFinite | Utility |
| integer | isInteger | Utility |
| arguments | isArguments | Utility |
| nil | isNil | Utility |
Contributing
Contributions are welcome! Please open an issue or submit a pull request.
