Taap
)A lightweight zero dependency Typescript type checking library that coerces unknown value types. More concise and accurate than typeof
checks or similar type checking methods.
npm i taap
import { isArray } from 'taap';
const maybeArray: unknown = [];
if (typeof maybeArray === 'array') {
🚫 typeof [] === 'object'
} else if (isArray(maybeArray)) {
// `maybeArray` is now of type: `any[]`
✅ maybeArray.push(1);
}
import { isArray } from 'taap';
const maybeArray: unknown = [1, 2, 3];
if (isArray<number>(maybeArray)) {
// `maybeArray` is now of type: `number[]`
maybeArray.filter((x) => x > 1);
}
Supports optional generic return type:
Fixed return type:
Other:
Accepts a type to narrow the return type.
Default: to any
.
Determine if argument is a BigInt
.
const maybeBigInt: unknown = BigInt(42);
if (isBigInt(maybeBigInt)) {
maybeBigInt === 42n;
}
Determine if argument is a boolean
.
const maybeBoolean: unknown = true;
if (isBoolean(maybeBoolean)) {
maybeBoolean === true
}
Determine if argument is a Date
.
const maybeDate: unknown = new Date('August 19, 1975 23:15:30');
if (isDate(maybeDate)) {
maybeDate.setFullYear(1969);
}
Determine if argument is a Function
.
const maybeFunction: unknown = (x: any) => x;
if (isFunction(maybeFunction)) {
maybeFunction.call(...args)
}
Provide a specific type:
const maybeFunction: unknown = (x: number) => x.toString();
if (isFunction<(x: number) => string>(maybeFunction)) {
maybeFunction(42) === '42'
}
Accepts a type to narrow the return type.
Determine if argument is null
.
const maybeNull: unknown = 1;
if (isNumber(maybeNull)) {
maybeNull === null;
}
Determine if argument is a number
.
const maybeNumber: unknown = 1;
if (isNumber(maybeNumber)) {
maybeNumber + 2 === 3;
}
Determine if argument is an Object
.
const maybeObject: unknown = { a: true };
if (isObject(maybeObject)) {
// `maybeObject` will be considered a Record<string, any>
maybeObject.someProp = true
}
Provide a specific type:
const maybeObject: unknown = { a: true };
if (isObject<{a: boolean}>(maybeObject)) {
// maybeObject will be typed as `{ a: boolean }`
maybeObject.a === true;
}
Accepts a type to narrow the return type.
Determine if argument is a RegEx
.
const maybeRegExp: unknown = /\w+\s/g;
if (isRegExp(maybeRegExp)) {
const str = 'fee fi fo fum';
maybeRegExp.test('fee fi fo fum') === true
}
Determine if argument is a string
.
const maybeString: unknown = 'ta,ap';
if (isString(maybeString)) {
maybeString.split(',').join('') === 'taap'
}
Determine if argument is undefined
.
const maybeUndefined: unknown = 1;
if (isNumber(maybeUndefined)) {
maybeUndefined === undefined;
}
Generated using TypeDoc
Determine if argument is an
Array
.const maybeArray: unknown = [1]; if (isArray(maybeArray)) { // `maybeArray` will be considered an Array maybeArray.map(x => x) }
Provide a specific type:
const maybeArray: unknown = [1]; if (isArray<number>(maybeArray)) { // `maybeArray` will be considered an Array and `x` will be considered a number maybeArray.map((x => x) // ` }