Runtime type checking for TypeScript (inspired by @sindresorhus/is)
- isomorphic (no DOM or NodeJS types)
- type guards (for type narrowing)
- fully tested (each possible type is tested on every type checker)
First, do yarn add is@npm:@alloc/is if you never use the npm CLI. Otherwise, you need to import @alloc/is instead of is.
import { is } from 'is' // // Get the type name of a value. // Object types are camel case. // isWhat(0) // 'number' isWhat({}) // 'Object' // // Check the constructor of a value. // isType(0, Number) // true isType({}, Object) // true isType([], Object) // false // // Find a constructor in a value's prototype chain. // isKind([], Object) // true // // Check if the value is a specific type. // isNumber(0) // true isArray([]) // trueSee the tests for expected behavior. They are very readable, just search for test( to jump between the tests of each is. function.
isWhat(value)Get the type name of a valueisType(value, constructor)Check the constructor of a valueisKind(value, constructor)Find a constructor in a value's prototype chainisArray(value)Same asArray.isArrayisAsyncFunction(value)isAsyncIterable(value)Returns true for objects returned bySymbol.asyncIteratorfunctionsisBigint(value)isBoolean(value)isClass(value)Returns true forclassfunctions (but not transpiled classes)isDate(value)isDefined(value)The opposite ofisUndefinedisEmptyObject(value)Returns true for plain objects with no keysisError(value)isGenerator(value)Returns true for objects returned by generator functionsisGeneratorFunction(value)isFunction(value)isInfinite(value)isInteger(value)isIterable(value)Returns true for objects returned bySymbol.iteratorfunctionsisMap(value)isNan(value)Same asNumber.isNaNisNull(value)isNumber(value)Returns true for any number (but neverNaN)isObject(value)Returns true for any object or function (but nevernull)isPlainObject(value)Returns true for objects created by{},new Object, orObject.create(null)isPromise(value)isPromiseLike(value)Returns true for objects with athenmethodisRegExp(value)isSafeInteger(value)Same asNumber.isSafeIntegerisSet(value)isString(value)isSymbol(value)isUndefined(value)isWeakMap(value)isWeakSet(value)