Options
All
  • Public
  • Public/Protected
  • All
Menu

Type Assertion And Predicate (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.

📚 Docs   âš¡ Demo   💻 Repo

github-workflows semantic-release styled with prettier

bundlephobia npm latest version npm next version

🛠 Getting Started

Install dependency

npm i taap

Import and use methods

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);
}

Optionally supply a return type

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);
}

🔭 Available Methods

📚Full documentation

Supports optional generic return type:

Fixed return type:

Other:

Index

Type aliases

TaapType

TaapType: "Array" | "Object" | "String" | "Date" | "RegExp" | "Function" | "Boolean" | "Number" | "BigInt" | "Null" | "Undefined"

Functions

getType

  • getType(v: any): "Array" | "Object" | "String" | "Date" | "RegExp" | "Function" | "Boolean" | "Number" | "BigInt" | "Null" | "Undefined"
  • Parameters

    • v: any

    Returns "Array" | "Object" | "String" | "Date" | "RegExp" | "Function" | "Boolean" | "Number" | "BigInt" | "Null" | "Undefined"

isArray

  • isArray<T>(val: any): val is Array<T>
  • 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) // `
    }

    Type parameters

    • T

      Accepts a type to narrow the return type.

      Default: to any.

    Parameters

    • val: any

    Returns val is Array<T>

isBigInt

  • isBigInt(val: any): val is BigInt
  • Determine if argument is a BigInt.

    const maybeBigInt: unknown = BigInt(42);
    
    if (isBigInt(maybeBigInt)) {
      maybeBigInt === 42n;
    }

    Parameters

    • val: any

    Returns val is BigInt

isBoolean

  • isBoolean(val: any): val is boolean
  • Determine if argument is a boolean.

    const maybeBoolean: unknown = true;
    
    if (isBoolean(maybeBoolean)) {
      maybeBoolean === true
    }

    Parameters

    • val: any

    Returns val is boolean

isDate

  • isDate(val: any): val is Date
  • Determine if argument is a Date.

    const maybeDate: unknown = new Date('August 19, 1975 23:15:30');
    
    if (isDate(maybeDate)) {
      maybeDate.setFullYear(1969);
    }

    Parameters

    • val: any

    Returns val is Date

isFunction

  • isFunction<T>(val: any): val is T
  • 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'
    }

    Type parameters

    • T

      Accepts a type to narrow the return type.

    Parameters

    • val: any

    Returns val is T

isNull

  • isNull(val: any): val is null
  • Determine if argument is null.

    const maybeNull: unknown = 1;
    
    if (isNumber(maybeNull)) {
      maybeNull === null;
    }

    Parameters

    • val: any

    Returns val is null

isNumber

  • isNumber(val: any): val is number
  • Determine if argument is a number.

    const maybeNumber: unknown = 1;
    
    if (isNumber(maybeNumber)) {
      maybeNumber + 2 === 3;
    }

    Parameters

    • val: any

    Returns val is number

isObject

  • isObject<T>(val: any): val is T
  • 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;
    }

    Type parameters

    • T: Record<string, any>

      Accepts a type to narrow the return type.

    Parameters

    • val: any

    Returns val is T

isRegExp

  • isRegExp(val: any): val is RegExp
  • 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
    }

    Parameters

    • val: any

    Returns val is RegExp

isString

  • isString(val: any): val is string
  • Determine if argument is a string.

    const maybeString: unknown = 'ta,ap';
    
    if (isString(maybeString)) {
      maybeString.split(',').join('') === 'taap'
    }

    Parameters

    • val: any

    Returns val is string

isUndefined

  • isUndefined(val: any): val is undefined
  • Determine if argument is undefined.

    const maybeUndefined: unknown = 1;
    
    if (isNumber(maybeUndefined)) {
      maybeUndefined === undefined;
    }

    Parameters

    • val: any

    Returns val is undefined

Legend

Generated using TypeDoc