Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

typedescriptor

thenativeweb343.7kMITdeprecated4.0.14TypeScript support: included

Package no longer supported. Contact Support at https://www.npmjs.com/support for more info.

typedescriptor identifies and describes types.

type, typeof

readme

typedescriptor

typedescriptor identifies and describes types.

Status

Category Status
Version npm
Dependencies David
Dev dependencies David
Build GitHub Actions
License GitHub

Installation

$ npm install typedescriptor

Quick Start

First you need to import the functions you are interested in:

const { 
  isArray,
  isBoolean,
  isError,
  isFunction,
  isMap,
  isNull,
  isNumber,
  isObject,
  isReference,
  isScalar,
  isSet,
  isString,
  isSymbol,
  isUndefined,
  typeOf
} = require('typedescriptor');

If you use TypeScript, use the following code instead:

import {
  isArray,
  isBoolean,
  isError,
  isFunction,
  isMap,
  isNull,
  isNumber,
  isObject,
  isReference,
  isScalar,
  isSet,
  isString,
  isSymbol,
  isUndefined,
  typeOf
} from 'typedescriptor';

Then, use the type-guards to determine a variable's type or to narrow a variable's type in TypeScript:

if (isNumber(value)) {
  // Do something with the number.
}

The types array, boolean, error, function, map, null, number, object, set, string, symbol and undefined are supported.

The isError implementation is re-exported from defekt.

Getting a variable's type name

To get a variable's type as a string, use typeOf:

const typeName = typeOf('foo');
//=> 'string'

This is not compatible with the builtin typeof operator. Most notably: null is not considered to be an object, since TypeScript differentiates between the two. null is considered a separate type.

Caveats

The isObject predicate overlaps with multiple others. If, for example, you want to treat a variable differently based on whether it is an array or any other object, you have to first check whether the variable is an array and only then check whether it is an object. If you would first check whether the variable is an object, the result would be true even for arrays.

const doStuff = function (value: any): void {
  if (isArray(value)) {
    // Do things with the array and return!
  }
  if (isObject(value)) {
    // Do things with the object, which now can not be an array.
  }
}

The same is true for isError, isMap and isSet.

Running the quality assurance

To run quality assurance for this module use roboter:

$ npx roboter

changelog

4.0.14 (2022-03-25)

Bug Fixes

4.0.13 (2022-03-24)

Bug Fixes

4.0.12 (2021-08-15)

Bug Fixes

4.0.11 (2021-07-26)

Bug Fixes

4.0.10 (2021-07-13)

Bug Fixes

4.0.9 (2021-07-06)

Bug Fixes

4.0.8 (2021-06-28)

Bug Fixes

4.0.7 (2021-06-10)

Bug Fixes

  • bump glob-parent from 5.1.1 to 5.1.2 (#310) (a2f4579)

4.0.6 (2021-06-10)

Bug Fixes

  • bump normalize-url from 6.0.0 to 6.0.1 (#311) (25eb88c)

4.0.5 (2021-06-10)

Bug Fixes

  • bump trim-newlines from 3.0.0 to 3.0.1 (#312) (fad917c)

4.0.4 (2021-05-31)

Bug Fixes

4.0.3 (2021-05-24)

Bug Fixes

4.0.2 (2021-05-17)

Bug Fixes

4.0.1 (2021-05-11)

Bug Fixes

  • bump hosted-git-info from 2.8.8 to 2.8.9 (#297) (abc2e94)

4.0.0 (2021-05-07)

Features

  • Rewrite library with focus on small type-guard functions. (#296) (91ef31d)

BREAKING CHANGES

  • The entire API is different. There is no Typedescriptor class anymore, just a bunch of type-guard functions and one function that returns a variable's type as a string.

3.0.2 (2021-03-25)

Bug Fixes

3.0.1 (2020-11-03)

Bug Fixes

  • Fix headline for robot section in readme. (#198) (2043e8b)