Skip to content

angular-package/type

 
 

Repository files navigation

Packages

Useful packages based on the angular.io.

Package Description Status Readme
change-detection Improve application performance. In Progress Readme
prism Prism highlighter module. In Progress Readme
property Features to handle object properties. In Progress Readme
ui User interface. In Progress Github
type Common types, type guards and type checkers. npm version Github | npm

angular-package/type

Common types, type guards and type checkers.

npm version GitHub issues GitHub forks GitHub stars GitHub license

// `guard` prefix functions.
import { 
  guardArray,
  guardBigInt,
  guardBoolean,
  guardClass,
  guardDefined,
  guardFunction,
  guardInstance,
  guardKey,
  guardNull,
  guardNumber,
  guardObject,
  guardObjectKey,
  guardObjectKeys,
  guardPrimitive,
  guardString,
  guardSymbol,
  guardType,
  guardUndefined
} from '@angular-package/type'; 
// `is` prefix functions.
import {
  isArray,
  isBigInt,
  isBoolean,
  isBooleanObject,
  isBooleanType,
  isClass,
  isDefined,
  isFunction,
  isInstance,
  isKey,
  isNull,
  isNumber,
  isNumberObject,
  isNumberType,
  isObject,
  isObjectKey,
  isObjectKeyIn,
  isObjectKeys,
  isPrimitive,
  isString,
  isStringObject,
  isStringType,
  isSymbol,
  isType,
  isUndefined
} from '@angular-package/type';
// `are` prefix functions.
import {
  areString
} from '@angular-package/type';
// `isNot` prefix functions.
import {
  isNotBoolean,
  isNotDefined,
  isNotFunction,
  isNotNull,
  isNotNumber,
  isNotString,
  isNotUndefined
} from '@angular-package/type';
// Object.
import { are, guard, is, isNot } from '@angular-package/type';
// Types.
import {
  AnyBoolean,
  AnyNumber,
  AnyString,
  Constructor,
  CycleHook,
  Defined,
  Func,
  Key,
  Never,
  NotUndefined,
  Primitive,
  Primitives,
  ResultCallback,
  Type,
  Types,
} from '@angular-package/type';

Features

How angular-package understands

Check

Is to check the inputted value to be the same as expected.

Type guard

Is to guard the parameter type to not let input unexpected value in the code editor.

Guard

Is a combination of both above to guard type in the code editor and to check inputted value.



Installation

Install @angular-package/type package with command:

npm i --save @angular-package/type

Skeleton

This package was built by the library skeleton which was generated with Angular CLI version 12.0.1.

Copy this package to the packages/type folder of the library skeleton then run the commands below.

Build

Run ng build type to build the package. The build artifacts will be stored in the dist/ directory.

Running unit tests

Run ng test type to execute the unit tests via Karma.

Callback

Default function to handle callback of ResultCallback type.

const resultCallback: ResultCallback = (result: boolean): boolean => result

Parameters:

Name: type Description
result: boolean A boolean type value from the result of the check
value?: any Any type value from the check

Returns:

Returns Type Description
boolean boolean The return type is a boolean

The return value is a boolean type result from the check.

Custom function to handle callback.

const customCallback: ResultCallback = (result: boolean, value: any): boolean => {
  if (result === false) {
    throw new Error(`${value} must be a string`);
  }
  return result;
};

const stringResult = isString('Lorem ipsum', customCallback);

Check

typeOf

Primary and common function in checking the types. Here is article about how it works.

const typeOf = (value: any): string => Object.prototype.toString.call(value).slice(8, -1).toLowerCase();

are

The object contains prefixed with are functions.

const are: Are = {
  string: areString
};

areString

Use areString() or are.string() to check if all the values are string.

const areString = (...value: any): boolean => check('string', ...value);

Parameters:

Name: type Description
...value: any A rest parameter of any type to check

Returns:

Returns Type Description
boolean boolean The return type is a boolean

The return value is a boolean indicating whether or not all the values are string.

Usage:

Example usage on playground


is

update

4.1.0: Adds objectKeys as isObjectKeys().

The object contains prefixed with is functions and prefixed with isNot functions in property not.

const is: Is = {
  array: isArray,
  bigInt: isBigInt, // deprecated
  bigint: isBigInt,
  boolean: isBoolean,
  booleanObject: isBooleanObject,
  booleanType: isBooleanType,
  class: isClass,
  defined: isDefined,
  function: isFunction,
  instance: isInstance,
  key: isKey,
  not: isNot,
  null: isNull,
  number: isNumber,
  numberObject: isNumberObject,
  numberType: isNumberType,
  object: isObject,
  objectKey: isObjectKey,
  objectKeyIn: isObjectKeyIn,
  objectKeys: isObjectKeys,
  primitive: isPrimitive,
  string: isString,
  stringObject: isStringObject,
  stringType: isStringType,
  symbol: isSymbol,
  type: isType,
  undefined: isUndefined
};

isArray

Use isArray() or is.array() to check if any value is an Array, Array instance, and object type.

const isArray: IsArray = <Type = any>(
  value: any,
  callback: ResultCallback = resultCallback
): value is Array<Type> =>
  callback(
    typeOf(value) === 'array' &&
    Array.isArray(value) === true &&
    value instanceof Array === true &&
    typeof value === 'object',
    value
  );

Generic type variables:

Name Default value Description
Type any A generic variable to the return type value is Array<Type>

Parameters:

Name: type Description
value: any Any value to check
callback: ResultCallback A ResultCallback type function, which by default is resultCallback() to handle the result before returns eg. to throw an Error

Returns:

Returns Type Description
value is Array<Type> boolean By default Type variable is equal to any and the return type is a boolean as the result of its statement indicating the value is an Array of any type

The return value is a boolean indicating whether or not the value is an Array.

Usage:

// Example usage
import { isArray } from '@angular-package/type';

const ARRAY_NUMBER = [1, 2, 3];
const ARRAY_STRING = ['a', 'b', 'c'];

isArray(ARRAY_NUMBER); // true
isArray<string>(ARRAY_STRING); // true

Example usage on playground


isBigInt

Use isBigInt() or is.bigint() to check if any value is a bigint type.

const isBigInt: IsBigInt = (
  value: any,
  callback: ResultCallback = resultCallback
): value is bigint =>
  callback(typeOf(value) === 'bigint' && typeof value === 'bigint', value);

Parameters:

Name: type Description
value: any Any value to check
callback: ResultCallback A ResultCallback type function, which by default is resultCallback() to handle the result before returns eg. to throw an Error

Returns:

Returns Type Description
value is bigint boolean The return type is a boolean as the result of its statement

The return value is a boolean indicating whether or not the value is a bigint.

Usage:

// Example usage
import { isBigInt } from '@angular-package/type';

const BIGINT = 9007199254740991n;
const NUMBER = 27;

isBigInt(NUMBER); // false
isBigInt(BIGINT); // true

Example usage on playground | How to detect bigint type


isBoolean

Use isBoolean() or is.boolean() to check if any value is a boolean type not instance of Boolean and Object or object type instance of Boolean and Object.

const isBoolean: IsBoolean = (
  value: any,
  callback: ResultCallback = resultCallback
): value is boolean =>
  callback(
    typeOf(value) === 'boolean' &&
    (isBooleanType(value) || isBooleanObject(value)),
    value
  );

Parameters:

Name: type Description
value: any Any value to check
callback: ResultCallback A ResultCallback type function, which by default is resultCallback() to handle the result before returns eg. to throw an Error

Returns:

Returns Type Description
value is boolean boolean The return type is a boolean as the result of its statement

The return value is a boolean indicating whether or not the value is a boolean type or Boolean instance.

Usage:

// Example usage
import { isBoolean } from '@angular-package/type';

const BOOLEAN = false;
const BOOLEAN_INSTANCE = new Boolean(false);

isBoolean(BOOLEAN); // true
isBoolean(BOOLEAN_INSTANCE); // true

Example usage on playground | How to detect the boolean type


isBooleanObject

Use isBooleanObject() or is.booleanObject() to check if any value is an object type and instance of Boolean and Object.

const isBooleanObject: IsBooleanObject = (
  value: any,
  callback: ResultCallback = resultCallback
): value is Boolean =>
  callback(
    typeof value === 'object' &&
    value instanceof Boolean === true &&
    value instanceof Object === true,
    value
  );

Parameters:

Name: type Description
value: any Any value to check
callback: ResultCallback A ResultCallback type function, which by default is resultCallback() to handle the result before returns eg. to throw an Error

Returns:

Returns Type Description
value is Boolean boolean The return type is a boolean as the result of its statement indicating the value is Boolean

The return value is a boolean indicating whether or not the value is a Boolean instance.

Usage:

// Example usage
import { isBooleanObject } from '@angular-package/type';

const BOOLEAN = false;
const BOOLEAN_INSTANCE = new Boolean(false);

isBooleanObject(BOOLEAN); // false
isBooleanObject(BOOLEAN_INSTANCE); // true

isBooleanType

Use isBooleanType() or is.booleanType() to check if any value is a boolean type not an instance of Boolean and Object, and equal to true or false.

const isBooleanType: IsBooleanType = (
  value: any,
  callback: ResultCallback = resultCallback
): value is boolean =>
  callback(
    value instanceof Boolean === false &&
    value instanceof Object === false &&
    typeof value === 'boolean' &&
    (value === true || value === false),
    value
  );

Parameters:

Name: type Description
value: any Any value to check
callback: ResultCallback A ResultCallback type function, which by default is resultCallback() to handle the result before returns eg. to throw an Error

Returns:

Returns Type Description
value is boolean boolean The return type is a boolean as the result of its statement

The return value is a boolean indicating whether or not the value is a boolean type.

Usage:

// Example usage
import { isBooleanType } from '@angular-package/type';

const BOOLEAN = false;
const BOOLEAN_INSTANCE = new Boolean(false);

isBooleanType(BOOLEAN); // true
isBooleanType(BOOLEAN_INSTANCE); // false

isClass

Use isClass() or is.class() to check if any value is a function type, an instance of Function and Object as a generic Class type of class. It also confirms with the regexp that function is a class.

const isClass: IsClass = <Class = Function>(
  value: any,
  callback: ResultCallback = resultCallback
): value is Class =>
  callback(
    typeOf(value) === 'function' &&
      typeof value === 'function' &&
      value instanceof Function === true &&
      value instanceof Object === true
      ? /class/.test(Function.prototype.toString.call(value).slice(0, 5))
      : false,
    value
  );

Generic type variables:

Name Default value Description
Class Function A generic variable to the return type value is Class

Parameters:

Name: type Description
value: any Any value to check
callback: ResultCallback A ResultCallback type function, which by default is resultCallback() to handle the result before returns eg. to throw an Error

Returns:

Returns Type Description
value is Class boolean By default Class variable is equal to Function and the return type is a boolean as the result of its statement indicating the value is a Function

The return value is a boolean indicating whether or not the value is a class.

Usage:

// Example usage
import { isClass } from '@angular-package/type';

class Class { x = 5; }
const FUNC: Func = (x: number): any => x + 5;

isClass(Class); // true
isClass(FUNC); // false
isClass(() => 5); // false

isDefined

fix

4.1.3: Fixes the return type boolean, which doesn't strictly indicate the value is defined by changing the value type to a generic Type and the return type to value is Defined<Type>.

Use isDefined() or is.defined() to check if a generic Type value is not an undefined type and is not equal to undefined.

const isDefined: IsDefined = <Type>(
  value: Type,
  callback: ResultCallback = resultCallback
): value is Defined<Type> =>
  callback(
    typeOf(value) !== 'undefined' &&
    typeof value !== 'undefined' &&
    value !== undefined,
    value
  );

Generic type variables:

Name Default value Description
Type From the value A generic Type variable to the return type value is Defined<Type>

Parameters:

Name: type Description
value: Type A generic Type value, by default of type detected from the value, to check
callback: ResultCallback A ResultCallback type function, which by default is resultCallback() to handle the result before returns eg. to throw an Error

Returns:

Returns Type Description
value is Defined<Type> boolean By default Type variable is equal to the type detected from the value, but the detected type undefined changes to never and the return type is a boolean as the result of its statement value is Defined<Type>

The return value is a boolean indicating whether or not the value is defined, not undefined

Usage:

// Example usage
import { isDefined } from '@angular-package/type';

const UNDEFINED = undefined;
let defined;

isDefined(UNDEFINED); // false
isDefined(defined); // false

isFunction

Use isFunction() or is.function() to check if any value is a function type, an instance of Function and Object. It also denies with the regexp that function is a class.

const isFunction: IsFunction = (
  value: any,
  callback: ResultCallback = resultCallback
): value is Func =>
  callback(
    typeOf(value) === 'function' &&
      typeof value === 'function' &&
      value instanceof Function === true &&
      value instanceof Object === true
      ? /class/.test(Function.prototype.toString.call(value).slice(0, 5)) ===
      false
      : false,
    value
  );

Parameters:

Name: type Description
value: any Any value to check
callback: ResultCallback A ResultCallback type function, which by default is resultCallback() to handle the result before returns eg. to throw an Error

Returns:

Returns Type Description
value is Func boolean The return type is a boolean as the result of its statement indicating the value is a Func

The return value is a boolean indicating whether or not the value is a function.

Usage:

// Example usage
import { isFunction } from '@angular-package/type';

type Func = (...param: any) => any;
class Class { x = 5; }
const FUNC: Func = (x: number): any => x + 5;

isFunction(Class); // true
isFunction(FUNC); // true
isFunction(() => 5); // true

Example usage on playground | How to detect function type


isInstance

update

4.1.1:

  • Changes the Class type variable name to Obj.
  • Changes the className parameter name to constructor cause it represents class or function constructor.
  • Fixes type variable Obj default value is set to Function by removing it cause it's always being picked from the constructor parameter.
  • Removes unnecessary Constructor from the return type.
  • Adds check constructor against the function.

Use isInstance() or is.instance() to check if any value is an object of a generic Obj type and an instance of Constructor type.

const isInstance: IsInstance = <Obj>(
  value: any,
  constructor: Constructor<Obj>,
  callback: ResultCallback = resultCallback
): value is Obj =>
  callback(
    isObject<Obj>(value)
      ? isClass<Obj>(constructor) || isFunction(constructor)
        ? value instanceof constructor === true
        : false
      : false,
    value
  );

Generic type variables:

Name Default value Description
Obj From the constructor A generic variable to the return type value is Obj

Parameters:

Name: type Description
value: any Any value to be an instance of the constructor
constructor: Constructor<Obj> A class or function that specifies the type of the Constructor
callback: ResultCallback A ResultCallback type function, which by default is resultCallback() to handle the result before returns eg. to throw an Error

Returns:

Returns Type Description
value is Obj boolean The return type is a boolean as the result of its statement indicating the value is Obj from the detected constructor type

The return value is a boolean indicating whether or not the value is an instance of a generic Obj.

Usage:

// Example usage
import { isInstance } from '@angular-package/type';

// Classes.
class Some { x = 127; }
class Two { y = 'Lorem ipsum'; }

const SOME = new Some();
const TWO = new Two();

isInstance<Some>(TWO, Some); // false
isInstance<Some>(SOME, Some); // true
isInstance<Some>(TWO, Two); // true and type error

// Function constructor.
function functionConstructor(this: any, ...args: any[]): any {
  if (args) {
    args.forEach((arg, index: number) => this[index] = arg[index]);
  }
  return this;
}

const anyInstance: any = new (functionConstructor as any)('First name', 'Sur name', 27);

isInstance(anyInstance, functionConstructor as any); // true

Example usage on playground | How to detect constructor instance


isKey

update

4.1.0: Checks only for primitive string and number.

Use isKey() or is.key() to check if any value is one of the string, number, or symbol.

const isKey: IsKey = (
  value: any,
  callback: ResultCallback = resultCallback
): value is Key =>
  callback(
    isStringType(value) || isNumberType(value) || isSymbol(value),
    value
  );

Parameters:

Name: type Description
value: any Any value to check
callback: ResultCallback A ResultCallback type function, which by default is resultCallback() to handle the result before returns eg. to throw an Error

Returns:

Returns Type Description
value is Key boolean The return type is a boolean as the result of its statement indicating the value is Key

The return value is a boolean indicating whether or not the value is a Key.

Usage:

// Example usage
import { isKey } from '@angular-package/type';

const STRING = 'surname';
const STRING_INSTANCE = new String(STRING);
isKey(STRING); // true
isKey(STRING_INSTANCE); // true

const NUMBER = 27;
const NUMBER_INSTANCE = new Number(NUMBER);
isKey(NUMBER); // true
isKey(NUMBER_INSTANCE); // true

const SYMBOL_NUMBER: unique symbol = Symbol(NUMBER);
const SYMBOL_STRING: unique symbol = Symbol(STRING);
isKey(SYMBOL_NUMBER); // true
isKey(SYMBOL_STRING); // true

isNull

Use isNull() or is.null() to check if any value is an object type and equal to null.

const isNull: IsNull = (
  value: any,
  callback: ResultCallback = resultCallback
): value is null =>
  callback(
    typeOf(value) === 'null' && typeof value === 'object' && value === null,
    value
  );

Parameters:

Name: type Description
value: any Any value to check
callback: ResultCallback A ResultCallback type function, which by default is resultCallback() to handle the result before returns eg. to throw an Error

Returns:

Returns Type Description
value is null boolean The return type is a boolean as the result of its statement

The return value is a boolean indicating whether or not the value is null.

Usage:

// Example usage
import { isNull } from '@angular-package/type';

/**
 * typeof === 'object'
 * instanceof Function === false
 * instanceof Number === false
 * instanceof Object === false
 */
const NULL: any = null;

/**
 * typeof === 'number'
 * instanceof Function === false
 * instanceof Number === false
 * instanceof Object === false
 */
const NUMBER = 27;

isNull(NULL); // true
isNull(NUMBER); // false

Example usage on playground | How to detect null type


isNumber

Use isNumber() or is.number() to check if any value is a number type not an instance of Number and Object or object type instance of Number and Object.

const isNumber: IsNumber = (
  value: any,
  callback: ResultCallback = resultCallback
): value is number =>
  callback(
    typeOf(value) === 'number' &&
    isFinite(value) === true &&
    (isNumberType(value) || isNumberObject(value)),
    value
  );

Parameters:

Name: type Description
value: any Any value to check
callback: ResultCallback A ResultCallback type function, which by default is resultCallback() to handle the result before returns eg. to throw an Error

Returns:

Returns Type Description
value is number boolean The return type is a boolean as the result of its statement

The return value is a boolean indicating whether or not the value is a number type or Number instance.

Usage:

Example usage on playground | How to detect a number type


isNumberObject

Use isNumberObject() or is.numberObject() to check if any value is an object type and instance of Number and Object.

const isNumberObject: IsNumberObject = (
  value: any,
  callback: ResultCallback = resultCallback
): value is Number =>
  callback(
    typeof value === 'object' &&
    value instanceof Number === true &&
    value instanceof Object === true,
    value
  );

Parameters:

Name: type Description
value: any Any value to check
callback: ResultCallback A ResultCallback type function, which by default is resultCallback() to handle the result before returns eg. to throw an Error

Returns:

Returns Type Description
value is Number boolean The return type is a boolean as the result of its statement indicating the value is Number

The return value is a boolean indicating whether or not the value is a Number instance.

Usage:

// Example usage
import { isNumberObject } from '@angular-package/type';

/**
 * typeof === 'number'
 * instanceof Function === false
 * instanceof Number === false
 * instanceof Object === false
 */
const NUMBER = 10304050;

/**
 * typeof === 'number'
 * instanceof Function === false
 * instanceof Number === false
 * instanceof Object === false
 */
const NUMBER_INSTANCE = Number(NUMBER);

/**
 * typeof === 'number'
 * instanceof Function === false
 * instanceof Number === true
 * instanceof Object === true
 */
const NUMBER_NEW_INSTANCE = new Number(NUMBER);

isNumberObject(NUMBER); // false
isNumberObject(NUMBER_INSTANCE); // false
isNumberObject(NUMBER_NEW_INSTANCE); // true

isNumberType

Use isNumberType() or is.numberType() to check if any value is a number type not an instance of Number and Object.

const isNumberType: IsNumberType = (
  value: any,
  callback: ResultCallback = resultCallback
): value is number =>
  callback(
    value instanceof Number === false &&
    value instanceof Object === false &&
    typeof value === 'number',
    value
  );

Parameters:

Name: type Description
value: any Any value to check
callback: ResultCallback A ResultCallback type function, which by default is resultCallback() to handle the result before returns eg. to throw an Error

Returns:

Returns Type Description
value is number boolean The return type is a boolean as the result of its statement

The return value is a boolean indicating whether or not the value is a number type.

Usage:

// Example usage
import { isNumberType } from '@angular-package/type';

/**
 * typeof === 'number'
 * instanceof Function === false
 * instanceof Number === false
 * instanceof Object === false
 */
const NUMBER = 10304050;

/**
 * typeof === 'number'
 * instanceof Function === false
 * instanceof Number === false
 * instanceof Object === false
 */
const NUMBER_INSTANCE = Number(NUMBER);

/**
 * typeof === 'number'
 * instanceof Function === false
 * instanceof Number === true
 * instanceof Object === true
 */
const NUMBER_NEW_INSTANCE = new Number(NUMBER);

isNumberType(NUMBER); // true
isNumberType(NUMBER_INSTANCE); // true
isNumberType(NUMBER_NEW_INSTANCE); // false

isObject

Use isObject() or is.object() to check if any value is an object of a generic Obj type and Object instance.

const isObject: IsObject = <Obj = object>(
  value: any,
  callback: ResultCallback = resultCallback
): value is Obj =>
  callback(
    typeOf(value) === 'object' &&
    typeof value === 'object' &&
    value instanceof Object === true,
    value
  );

Generic type variables:

Name Default value Description
Obj object A generic variable to the return type value is Obj

Parameters:

Name: type Description
value: any Any value to check
callback: ResultCallback A ResultCallback type function, which by default is resultCallback() to handle the result before returns eg. to throw an Error

Returns:

Returns Type Description
value is Obj boolean By default Obj variable is equal to object and the return type is a boolean as the result of its statement indicating the value is object

The return value is a boolean indicating whether or not the value is an object.

Usage:

// Example usage
import { isObject } from '@angular-package/type';

/**
 * typeof === 'number'
 * instanceof Function === false
 * instanceof Number === false
 * instanceof Object === false
 */
const x = 10304050;

/**
 * typeof === 'number'
 * instanceof Function === false
 * instanceof Number === false
 * instanceof Object === false
 */
const NUMBER = 10304050;

/**
 * typeof === 'string'
 * instanceof Function === false
 * instanceof Object === false
 * instanceof String === false
 */
const STRING = '!@#$%^&*()abcdefghijklmnoprstuwyz';

const SYMBOL_NUMBER: unique symbol = Symbol(NUMBER);
const SYMBOL_STRING: unique symbol = Symbol(STRING);

interface ObjectOne {
  'key as string'?: boolean;
  1030405027?: string;
  5?: string;
  [number]: number;
  [STRING]: string;
  [SYMBOL_NUMBER]?: string;
  [SYMBOL_STRING]?: number;
  x: number;
}

const OBJECT_ONE: ObjectOne = {
  'key as string': true,
  1030405027: 'key is number',
  5: 'key is also number',
  [number]: NUMBER,
  [STRING]: 'key is string',
  [SYMBOL_NUMBER]: 'key is symbol number',
  [SYMBOL_STRING]: 6,
  x: 3000
};

isObject(OBJECT_ONE); // true

Example usage on playground | How to detect an object type


isObjectKey

Use isObjectKey() or is.objectKey() to check if any value is an object with its own specified keys of the Key. The function uses hasOwnProperty Object method to finds enumerable and non-enumerable Key as string, number, symbol unlike Object.keys() but it can't find accessor descriptor property unlike in operator, which can.

const isObjectKey: IsObjectKey = <Type = object>(
  value: any,
  key: Key | Key[],
  callback: ResultCallback = resultCallback
): value is Type =>
  callback(
    isObject<Type>(value)
      ? isArray(key)
        ? key.every((k) =>
          isKey(k) ? {}.hasOwnProperty.call(value, k) === true : false
        )
        : isKey(key)
          ? {}.hasOwnProperty.call(value, key)
          : false
      : false,
    value
  );

Generic type variables:

Name Default value Description
Type object A generic variable to the return type value is Type

Parameters:

Name: type Description
value: any Any value to check if it contains a specified key
key: Key | Key[] A Key type or an array of Key type to check in the value
callback: ResultCallback A ResultCallback type function, which by default is resultCallback() to handle the result before returns eg. to throw an Error

Returns:

Returns Type Description
value is Type boolean By default Type variable is equal to object and the return type is a boolean as the result of its statement indicating the value is object

The return value is a boolean indicating whether or not the value is an object with its own specified keys.

Usage:

// Example usage
import { isObjectKey } from '@angular-package/type';

/**
 * typeof === 'number'
 * instanceof Function === false
 * instanceof Number === false
 * instanceof Object === false
 */
const NUMBER = 10304050;

 /**
  * typeof === 'string'
  * instanceof Function === false
  * instanceof Object === false
  * instanceof String === false
  */
const STRING = '!@#$%^&*()abcdefghijklmnoprstuwyz';

const SYMBOL_NUMBER: unique symbol = Symbol(NUMBER);
const SYMBOL_STRING: unique symbol = Symbol(STRING);

interface ObjectOne {
  'key as string'?: boolean;
  1030405027?: string;
  5?: string;
  [number]: number;
  [STRING]: string;
  [SYMBOL_NUMBER]?: string;
  [SYMBOL_STRING]?: number;
  x: number;
}

/**
 * typeof === 'object'
 * instanceof Boolean === false
 * instanceof Function === false
 * instanceof Number === false
 * instanceof Object === true
 * instanceof String === false
 * instanceof Symbol === false
 */
const OBJECT_ONE: ObjectOne = {
  'key as string': true,
  1030405027: 'key is number',
  5: 'key is also number',
  [number]: NUMBER,
  [STRING]: 'key is string',
  [SYMBOL_NUMBER]: 'key is symbol number',
  [SYMBOL_STRING]: 6,
  x: 3000
};

isObjectKey(OBJECT_ONE, STRING); // true
isObjectKey(OBJECT_ONE, 1030405027); // true
isObjectKey(OBJECT_ONE, NUMBER); // true
isObjectKey(OBJECT_ONE, SYMBOL_NUMBER); // true
isObjectKey(OBJECT_ONE, SYMBOL_STRING); // true

/**
 * typeof === 'function'
 * instanceof Class === false
 * instanceof Function === true
 * instanceof Object === true
 */
class Class {

  1030405027 = 'my new number';
  5 = 'my number';

  firstName = 'My name';
  surname = 'Surname';

  x = NUMBER;
  y = STRING;

  get [number](): number {
    return this.x;
  }
  get [STRING](): string {
    return this.y;
  }

  get [SYMBOL_NUMBER](): number {
    return this.x;
  }

  get [SYMBOL_STRING](): string {
    return this.y;
  }
}

/**
 * typeof === 'object'
 * instanceof Class === true
 * instanceof Function === false
 * instanceof Object === true
 */
const CLASS = new Class();

// One of the differences between the `in` operator and the `hasOwnProperty()` method is that it doesn't find a getter key
isObjectKey(CLASS, SYMBOL_NUMBER); // false
isObjectKey(CLASS, SYMBOL_STRING); // false
isObjectKey(CLASS, [SYMBOL_NUMBER, SYMBOL_STRING]); // false

isObjectKeyIn

Use isObjectKeyIn() or is.objectKeyIn() to check if any value is an Object with the key of the Key type by using the in operator. The function uses operator in to find the key.

const isObjectKeyIn: IsObjectKeyIn = <Type = object>(
  value: any,
  key: Key | Key[],
  callback: ResultCallback = resultCallback
): value is Type =>
  callback(
    isObject<Type>(value)
      ? isArray(key)
        ? key.every((k) => (isKey(k) ? k in value : false))
        : isKey(key)
          ? key in value
          : false
      : false,
    value
  );

Generic type variables:

Name Default value Description
Type object A generic variable to the return type value is Type

Parameters:

Name: type Description
value: any Any value to check if it contains a specified key
key: Key | Key[] A Key type or an array of Key type to check in the value
callback: ResultCallback A ResultCallback type function, which by default is resultCallback() to handle the result before returns eg. to throw an Error

Returns:

Returns Type Description
value is Type boolean By default Type variable is equal to object and the return type is a boolean as the result of its statement indicating the value is object

The return value is a boolean indicating whether or not the value is an object with the keys.

Usage:

import { isObjectKeyIn } from '@angular-package/type';

/**
 * typeof === 'number'
 * instanceof Function === false
 * instanceof Number === false
 * instanceof Object === false
 */
const NUMBER = 10304050;

/**
 * typeof === 'string'
 * instanceof Function === false
 * instanceof Object === false
 * instanceof String === false
 */
const STRING = '!@#$%^&*()abcdefghijklmnoprstuwyz';

/**
 * typeof === 'symbol'
 * instanceof Boolean === false
 * instanceof Function === false
 * instanceof Number === false
 * instanceof Object === false
 * instanceof String === false
 * instanceof Symbol === false
 */
const SYMBOL_NUMBER: unique symbol = Symbol(NUMBER);
const SYMBOL_STRING: unique symbol = Symbol(STRING);

/**
 * typeof === 'function'
 * instanceof Class === false
 * instanceof Function === true
 * instanceof Object === true
 */
class Class {

   1030405027 = 'my new number';
   5 = 'my number';

   firstName = 'My name';
   surname = 'Surname';

   x = NUMBER;
   y = STRING;

   get [number](): number {
     return this.x;
   }
   get [STRING](): string {
     return this.y;
   }

   get [SYMBOL_NUMBER](): number {
     return this.x;
   }

   get [SYMBOL_STRING](): string {
     return this.y;
   }
}

/**
 * typeof === 'object'
 * instanceof Class === true
 * instanceof Function === false
 * instanceof Object === true
 */
const CLASS = new Class();

// One of the differences between `in` operator and the `hasOwnProperty()` method is that it finds a getter key
isObjectKeyIn(CLASS, SYMBOL_NUMBER); // true
isObjectKeyIn(CLASS, SYMBOL_STRING); // true
isObjectKeyIn(CLASS, [SYMBOL_NUMBER, SYMBOL_STRING]); // true

isObjectKeys

new

Use isObjectKeys() or is.objectKeys() to check if any value is an object with some its own specified keys of the Key. Cause of using some() on the rest parameter ...keys each of its argument is treated as logic or, and cause of using every() on its array argument each of array item is treated as logic and. Simply, in the usage section below the function finds in the object get and set or writable and value, which means the object contains get and set or writable and value. The function uses hasOwnProperty Object method to finds enumerable and non-enumerable Key as string, number, symbol unlike Object.keys() but it can't find accessor descriptor property unlike in operator, which can.

const isObjectKeys: IsObjectKeys = <Type = object>(
  value: any,
  ...keys: (Key | Array<Key>)[]
): value is Type =>
  isObject<Type>(value)
    ? keys.some((key) =>
      isArray(key)
        ? key.every((k) =>
          isKey(k) ? {}.hasOwnProperty.call(value, k) === true : false
        )
        : isKey(key)
          ? {}.hasOwnProperty.call(value, key) === true
          : false
    )
    : false;

Generic type variables:

Name Default value Description
Type object A generic variable to the return type value is Type

Parameters:

Name: type Description
value: any Any value to check if it contains some of the specified keys
...keys: (Key | Array<Key>)[] A rest parameter single Key type or an array of Key type to check in the value

Returns:

Returns Type Description
value is Type boolean By default Type variable is equal to object and the return type is a boolean as the result of its statement indicating the value is object

The return value is a boolean indicating whether or not the value is an object with some its own specified keys.

Usage:

// Real example usage from incoming @angular-package/property.
interface CommonDescriptor extends Pick<PropertyDescriptor, 'configurable' | 'enumerable'> {}
interface AccessorDescriptor<Value> extends CommonDescriptor {
  get: (() => Value) | undefined;
  set: ((value: Value) => void) | undefined;
}
interface DataDescriptor<Value> extends CommonDescriptor {
  writable: boolean;
  value: Value;
}
type ThisAccessorDescriptor<Value, Obj = any> = AccessorDescriptor<Value> & ThisType<Obj>;

const ACCESSOR_DESCRIPTOR: ThisAccessorDescriptor<string | undefined, ObjectOne> =  {
  configurable: true,
  enumerable: true,
  get(): string | undefined {
    return this[5];
  },
  set(value: string | undefined) {
    this[5] = value;
  }
}

const DATA_DESCRIPTOR: DataDescriptor<string> = {
  configurable: true,
  enumerable: true,
  writable: true,
  value: 'my value'
}

isObjectKeys(ACCESSOR_DESCRIPTOR, ['writable', 'value'], ['get', 'set']); // true
isObjectKeys(ACCESSOR_DESCRIPTOR, ['writable', 'value']); // false
isObjectKeys(ACCESSOR_DESCRIPTOR, 'writable', 'value'); // false
isObjectKeys(ACCESSOR_DESCRIPTOR, ['get', 'set']); // true
isObjectKeys(ACCESSOR_DESCRIPTOR, 'get', 'set'); // true

isObjectKeys(DATA_DESCRIPTOR, ['writable', 'value'], ['get', 'set']); // true
isObjectKeys(DATA_DESCRIPTOR, ['writable', 'value']); // true
isObjectKeys(DATA_DESCRIPTOR, 'writable', 'value'); // true
isObjectKeys(DATA_DESCRIPTOR, ['get', 'set']); // false
isObjectKeys(DATA_DESCRIPTOR, 'get', 'set'); // false

isPrimitive

Use isPrimitive() or is.primitive() to check if any value is the Primitive type from a type of the Primitives type.

const isPrimitive: IsPrimitive = <T extends Primitive>(
  value: any,
  type: Primitives,
  callback: ResultCallback = resultCallback
): value is T => {
  if (isStringType(type)) {
    switch (type) {
      case 'bigint': return isBigInt(value, callback);
      case 'boolean': return isBooleanType(value, callback);
      case 'number': return isNumberType(value, callback);
      case 'null': return isNull(value, callback);
      case 'string': return isStringType(value, callback);
      case 'symbol': return isSymbol(value, callback);
      case 'undefined': return isUndefined(value, callback);
    }
  }
  return false;
};

Generic type variables:

Name Default value Description
T extends Primitive Primitive Guarded with Primitive type, T variable to the return type value is T

Parameters:

Name: type Description
value: any Any value to check if it's a Primitive from the type
type: Primitives A string type from the Primitives to check the value
callback: ResultCallback A ResultCallback type function, which by default is resultCallback() to handle the result before returns eg. to throw an Error

Returns:

Returns Type Description
value is T boolean By default T variable is equal to Primitive and the return type is a boolean as the result of its statement indicating the value is Primitive

The return value is a boolean indicating whether or not the value is a type from the Primitives.

Usage:

Example usage on playground


isString

Use isString() or is.string() to check if any value is a string type, not instance of Object and String or object type and instance of String and Object.

const isString: IsString = (
  value: any,
  callback: ResultCallback = resultCallback
): value is string =>
  callback(
    typeOf(value) === 'string' &&
    (isStringType(value) || isStringObject(value)),
    value
  );

Parameters:

Name: type Description
value: any Any value to check
callback: ResultCallback A ResultCallback type function, which by default is resultCallback() to handle the result before returns eg. to throw an Error

Returns:

Returns Type Description
value is string boolean The return type is a boolean as the result of its statement

The return value is a boolean indicating whether or not the value is a string type or String instance.


isStringObject

Use isStringObject() or is.stringObject() to check if any value is an object type and instance of String and Object.

const isStringObject: IsStringObject = (
  value: any,
  callback: ResultCallback = resultCallback
): value is String =>
  callback(
    value instanceof Object === true &&
    value instanceof String === true &&
    typeof value === 'object',
    value
  );

Parameters:

Name: type Description
value: any Any value to check
callback: ResultCallback A ResultCallback type function, which by default is resultCallback() to handle the result before returns eg. to throw an Error

Returns:

Returns Type Description
value is String boolean The return type is a boolean as the result of its statement indicating the value is String

The return value is a boolean indicating whether or not the value is a String instance.


isStringType

Use isStringType() or is.stringType() to check if any value is a string type and not instance of String and Object.

const isStringType: IsStringType = (
  value: any,
  callback: ResultCallback = resultCallback
): value is string =>
  callback(
    value instanceof Object === false &&
    value instanceof String === false &&
    typeof value === 'string',
    value
  );

Parameters:

Name: type Description
value: any Any value to check
callback: ResultCallback A ResultCallback type function, which by default is resultCallback() to handle the result before returns eg. to throw an Error

Returns:

Returns Type Description
value is string boolean The return type is a boolean as the result of its statement

The return value is a boolean indicating whether or not the value is a string type.


isSymbol

Use isSymbol() or is.symbol() to check if any value is a symbol type.

const isSymbol: IsSymbol = (
  value: any,
  callback: ResultCallback = resultCallback
): value is symbol =>
  callback(typeOf(value) === 'symbol' && typeof value === 'symbol', value);

Parameters:

Name: type Description
value: any Any value to check
callback: ResultCallback A ResultCallback type function, which by default is resultCallback() to handle the result before returns eg. to throw an Error

Returns:

Returns Type Description
value is symbol boolean The return type is a boolean as the result of its statement

The return value is a boolean indicating whether or not the value is a symbol.

Usage:

Example usage on playground | How to detect symbol type


isType

Use isType() or is.type() to check if any value is the Type from a type of the Types type.

const isType: IsType = <T extends Type>(
  value: any,
  type: Types<T>,
  callback: ResultCallback = resultCallback
): value is T => {
  if (isStringType(type)) {
    switch (type) {
      // Primitives.
      case 'bigint':
      case 'boolean':
      case 'number':
      case 'null':
      case 'string':
      case 'symbol':
      case 'undefined': return isPrimitive(value, type, callback);
      // Function.
      case 'function': return isFunction(value, callback);
      // Object.
      case 'object': return isObject<T>(value);
    }
  } else if (isNotNull(type)) {
    return isInstance<T>(value, type, callback);
  }
  return false;
};

Generic type variables:

Name Default value Description
T extends Type Type Guarded with Type, T variable to the return type value is T

Parameters:

Name: type Description
value: any Any value to check if its type is from the type
type: Types<T> A string or generic Constructor<T> type from the Types to check the value
callback: ResultCallback A ResultCallback type function, which by default is resultCallback() to handle the result before returns eg. to throw an Error

Returns:

Returns Type Description
value is T boolean By default T variable is equal to Type, but the detected class changes it to the class name and the return type is a boolean as the result of its statement indicating the value is Type

The return value is a boolean indicating whether or not the value is the Type from a type of the Types.

Usage:

Example usage on playground


isUndefined

Use isUndefined() or is.undefined() to check if any value is an undefined type and equal to undefined.

const isUndefined: IsUndefined = (
  value: any,
  callback: ResultCallback = resultCallback
): value is undefined =>
  callback(
    typeOf(value) === 'undefined' &&
    typeof value === 'undefined' &&
    value === undefined,
    value
  );

Parameters:

Name: type Description
value: any Any value to check
callback: ResultCallback A ResultCallback type function, which by default is resultCallback() to handle the result before returns eg. to throw an Error

Returns:

Returns Type Description
value is undefined boolean The return type is a boolean as the result of its statement

The return value is a boolean indicating whether or not the value is undefined.

Usage:

Example usage on playground | How to detect undefined type


isNot

Object isNot with all check is not functions.

const isNot: IsNot = {
  boolean: isNotBoolean,
  defined: isNotDefined,
  function: isNotFunction,
  null: isNotNull,
  number: isNotNumber,
  string: isNotString,
  undefined: isNotUndefined
};

isNotBoolean

fix

4.1.3: Fixes the return type boolean, which doesn't strictly indicate the value is not boolean or Boolean by changing the value type to a generic Type and the return type to value is Never<AnyBoolean, Type>.

Use isNotBoolean() or is.not.boolean() to check if a generic Type value is not a boolean type and not an instance of a Boolean.

const isNotBoolean: IsNotBoolean = <Type>(
  value: Type,
  callback: ResultCallback = resultCallback
): value is Never<AnyBoolean, Type> =>
  callback(
    typeOf(value) !== 'boolean' &&
    typeof value !== 'boolean' &&
    value instanceof Boolean === false,
    value
  );

Generic type variables:

Name Default value Description
Type From the value A generic Type variable from the value to the return type value is Never<AnyBoolean, Type>

Parameters:

Name: type Description
value: Type A generic Type value, by default of type detected from the value, to check
callback: ResultCallback A ResultCallback type function, which by default is resultCallback() to handle the result before returns eg. to throw an Error

Returns:

Returns Type Description
value is Never<AnyBoolean, Type> boolean By default Type variable is equal to the type detected from the value, but the detected type boolean or Boolean changes to never and the return type is a boolean as the result of its statement value is never of AnyBoolean type but of type detected from the value

The return value is a boolean indicating whether or not the value is not a boolean and Boolean instance.

Usage:

// Example usage.
import { isNotBoolean } from '@angular-package/type';

const anyBoolean: any = true;
const strictBoolean = false;
const objectBoolean = new Boolean(strictBoolean);

isNotBoolean(anyBoolean); // false; return type is `value is any`
isNotBoolean(strictBoolean); // false; return type is `value is never`
isNotBoolean(objectBoolean); // false; return type is `value is never`

isNotDefined

fix

4.1.3: Fixes the return type boolean, which doesn't strictly indicate the value is not defined by changing the value type to a generic Type and the return type to value is Undefined<Type>.

Use isNotDefined() or is.not.defined() to check if a generic Type value is an undefined type and is equal to undefined.

const isNotDefined: IsNotDefined = <Type>(
  value: Type,
  callback: ResultCallback = resultCallback
): value is Undefined<Type> =>
  callback(
    typeOf(value) === 'undefined' &&
    typeof value === 'undefined' &&
    value === undefined,
    value
  );

Generic type variables:

Name Default value Description
Type From the value A generic Type variable from the value to the return type value is Undefined<Type>

Parameters:

Name: type Description
value: Type A generic Type value, by default of type detected from the value, to check
callback: ResultCallback A ResultCallback type function, which by default is resultCallback() to handle the result before returns eg. to throw an Error

Returns:

Returns Type Description
value is Undefined<Type> boolean By default Type variable is equal to the type detected from the value, but the detected type other than undefined changes to never and the return type is a boolean as the result of its statement value is never of any other type than undefined

The return value is a boolean indicating whether or not the value is not defined, is undefined.

Usage:

// Example usage.
import { isNotDefined } from '@angular-package/type';

const anyUndefined: any = undefined;
const firstName = undefined;
const surname = 'My last name ';

isNotDefined(anyUndefined); // true; return type is `value is any`
isNotDefined(firstName); // true;  return type is `value is undefined`
isNotDefined(surname); // false; return type is `value is never`

isNotFunction

fix

4.1.3: Fixes the return type boolean, which doesn't strictly indicate the value is not function by changing the value type to a generic Type and the return type to value is Never<Func, Type>.

Use isNotFunction() or is.not.function() to check if a generic Type value is not a function type and not an instance of Function.

const isNotFunction: IsNotFunction = <Type>(
  value: Type,
  callback: ResultCallback = resultCallback
): value is Never<Func, Type> =>
  callback(
    typeOf(value) !== 'function' &&
    typeof value !== 'function' &&
    value instanceof Function === false,
    value
  );

Generic type variables:

Name Default value Description
Type From the value A generic Type variable from the value to the return type value is Never<Func, Type>

Parameters:

Name: type Description
value: Type A generic Type value, by default of type detected from the value, to check
callback: ResultCallback A ResultCallback type function, which by default is resultCallback() to handle the result before returns eg. to throw an Error

Returns:

Returns Type Description
value is Never<Func, Type> boolean By default Type variable is equal to the type detected from the value, but the detected type Func changes to never and the return type is a boolean as the result of its statement value is never Func but of type detected from the value

The return value is a boolean indicating whether or not the value is not a function.

Usage:

// Example usage.
import { IsNotFunction } from '@angular-package/type';

const anyFunc: any = (x: number) => x + 5;
const myFunc: Func = (x: string) => x;

isNotFunction(anyFunc); // false; return type is `value is any`
isNotFunction(myFunc); // false; return type is `value is never`
isNotFunction('maybe i am not a function'); // true; return type is `value is string`

isNotNull

fix

4.1.3: Fixes the return type boolean, which doesn't strictly indicate the value is not null by changing value type to a generic Type and the return type to value is Never<null, Type>.

Use isNotNull() or is.not.null() to check if a generic Type value is not a null type and not equal to null.

const isNotNull: IsNotNull = <Type>(
  value: Type,
  callback: ResultCallback = resultCallback
): value is Never<null, Type> =>
  callback(typeOf(value) !== 'null' && value !== null, value);

Generic type variables:

Name Default value Description
Type From the value A generic Type variable from the value to the return type value is Never<null, Type>

Parameters:

Name: type Description
value: Type A generic Type value, by default of type detected from the value to check
callback: ResultCallback A ResultCallback type function, which by default is resultCallback() to handle the result before returns eg. to throw an Error

Returns:

Returns Type Description
value is Never<null, Type> boolean By default Type variable is equal to the type detected from the value, but the detected type null changes to never and the return type is a boolean as the result of its statement value is never null but of type detected from the value

The return value is a boolean indicating whether or not the value is not null.

Usage:

// Example usage
import { isNotNull } from '@angular-package/type';

const anyNull: any = null;
const firstName = null;

isNotNull(anyNull); // return type is `value is any`
isNotNull(firstName); // return type is `value is never`

isNotNumber

fix

4.1.3: Fixes the return type boolean, which doesn't strictly indicate the value is not a number or Number by changing value type to a generic Type and the return type to value is Never<number, Type>.

Use isNotNumber() or is.not.number() to check if a generic Type value is not a number type and not an instance of Number.

const isNotNumber: IsNotNumber = <Type>(
  value: Type,
  callback: ResultCallback = resultCallback
): value is Never<AnyNumber, Type> =>
  callback(
    typeOf(value) !== 'number' &&
    typeof value !== 'number' &&
    value instanceof Number === false,
    value
  );

Generic type variables:

Name Default value Description
Type From the value A generic Type variable from the value to the return type value is Never<AnyNumber, Type>

Parameters:

Name: type Description
value: Type A generic Type, by default of type detected from the value to check
callback: ResultCallback A ResultCallback type function, which by default is resultCallback() to handle the result before returns eg. to throw an Error

Returns:

Returns Type Description
value is Never<AnyNumber, Type> boolean By default Type variable is equal to the type detected from the value, but the detected type number or Number changes to never and the return type is a boolean as the result of its statement value is never AnyNumber but of type detected from the value

The return value is a boolean indicating whether or not the value is not a number and Number instance.

Usage:

// Example usage
import { isNotNumber } from '@angular-package/type';

const anyNumber: any = 'any number';
const firstName = 'firstName';
const age = 27;
const objectNumber = new Number(927);

isNotNumber(anyNumber); // return type is `value is any`
isNotNumber(firstName); // return type is `value is string`
isNotNumber(age); // return type is `value is never`
isNotNumber(objectNumber); // return type is `value is never`

isNotString

fix

4.1.3: Fixes the return type boolean, which doesn't strictly indicate the value is not a string or String by changing value type to a generic Type and the return type to value is Never<string, Type>.

Use isNotString() or is.not.string() to check if a generic Type value is not a string type and not an instance of String.

const isNotString: IsNotString = <Type>(
  value: Type,
  callback: ResultCallback = resultCallback
): value is Never<AnyString, Type> =>
  callback(
    typeOf(value) !== 'string' &&
    typeof value !== 'string' &&
    value instanceof String === false,
    value
  );

Generic type variables:

Name Default value Description
Type From the value A generic Type variable from the value to the return type value is Never<AnyString, Type>

Parameters:

Name: type Description
value: Type A generic Type, by default of type detected from the value to check
callback: ResultCallback A ResultCallback type function, which by default is resultCallback() to handle the result before returns eg. to throw an Error

Returns:

The function returns statement value is Never<AnyString, Type>.

Type Description
boolean By default Type variable is equal to the type detected from the value, but the detected type string or String changes to never and the return type is a boolean as the result of its statement value is never AnyString but of type detected from the value

The return value is a boolean indicating whether or not the value is not a string and String instance.

Usage:

// Example usage
import { isNotString } from '@angular-package/type';

const anyString: any = 'any string';
const firstName = 'firstName';
const age = 27;
const objectString = new String('hold me');

isNotString(anyString); // return type is `value is any`
isNotString(firstName); // return type is `value is never`
isNotString(age); // return type is `value is number`
isNotString(objectString); // return type is `value is never`

isNotUndefined

fix

4.1.3: Fixes the return type boolean, which doesn't strictly indicate the value is not undefined by changing value type to a generic Type and the return type to value is Defined<Type>.

Use isNotUndefined() or is.not.undefined() to check if a generic Type value is not an undefined type and not equal to undefined.

const isNotUndefined: IsNotUndefined = <Type>(
  value: Type,
  callback: ResultCallback = resultCallback
): value is Defined<Type> =>
  callback(
    typeOf(value) !== 'undefined' &&
    typeof value !== 'undefined' &&
    value !== undefined,
    value
  );

Parameters:

Name: type Description
value: Type A generic Type value, by default of type detected from the value, to check
callback: ResultCallback A ResultCallback type function, which by default is resultCallback() to handle the result before returns eg. to throw an Error

Returns:

Returns Type Description
value is Defined<Type> boolean By default Type variable is equal to the type detected from the value, but the detected type undefined changes to never and the return type is a boolean as the result of its statement value is defined of type detected from the value

The return value is a boolean indicating whether or not the value is not undefined.

Usage:

// Example usage.
import { is } from '@angular-package/type';

interface Config {
  a?: string;
  b?: string;
}
const config: Config = {
  a: 'x',
  b: 'y'
};

function configFunction(value: string): string {
  return '';
}

if (is.not.undefined(config.a)) {
  configFunction(config.a);
}

Guard

guard

update

4.1.0: Adds objectKeys as guardObjectKeys().

The object contains prefixed with guard functions in is property.

const guardIs: GuardIs = {
  array: guardArray,
  bigint: guardBigInt,
  boolean: guardBoolean,
  class: guardClass,
  defined: guardDefined,
  function: guardFunction,
  instance: guardInstance,
  key: guardKey,
  null: guardNull,
  number: guardNumber,
  object: guardObject,
  objectKey: guardObjectKey,
  objectKeys: guardObjectKeys,
  primitive: guardPrimitive,
  string: guardString,
  symbol: guardSymbol,
  type: guardType,
  undefined: guardUndefined
};
const guard: Guard = {
  is: guardIs
};

guardArray

Use guardArray() or guard.is.array() to guard the value to be an Array of a generic Type.

const guardArray: GuardArray = <Type>(
  value: Array<Type>,
  callback?: ResultCallback
): value is Array<Type> => isArray<Type>(value, callback);

Generic type variables:

Name Default value Description
Type From the value An Array of a Type variable from the value to the return type value is Array<Type>

Parameters:

Name: type Description
value: Array<Type> An Array of a generic Type, by default of type detected from the value - to guard
callback?: ResultCallback An optional ResultCallback type function to handle the result before returns eg. to throw an Error

Returns:

Returns Type Description
value is Array<Type> boolean By default Type variable is equal to the type detected from the value and the return type is a boolean as the result of its statement indicating the value is an Array of Type detected from the value

The return value is a boolean indicating whether or not the value is an Array.

Usage:

Example usage on playground


guardBigInt

Use guardBigInt() or guard.is.bigint() to guard the value to be a bigint.

const guardBigInt: GuardBigInt = (
  value: bigint,
  callback?: ResultCallback
): value is bigint => isBigInt(value, callback);

Parameters:

Name: type Description
value: bigint A bigint type value to guard
callback?: ResultCallback An optional ResultCallback type function to handle the result before returns eg. to throw an Error

Returns:

Type Description
bigint The return value is a boolean indicating whether or not the value is a bigint

guardBoolean

Use guardBoolean() or guard.is.boolean() to guard the value to be any type of a boolean.

const guardBoolean: GuardBoolean = <B extends AnyBoolean>(
  value: B,
  callback?: ResultCallback
): value is B => isBoolean(value, callback);

Generic type variables:

Name Default value Description
B extends AnyBoolean From the value Guarded with AnyBoolean, generic B variable from the value to the return type value is B

Parameters:

Name: type Description
value: B An AnyBoolean type value, by default of a generic B type detected from the value - to guard
callback?: ResultCallback An optional ResultCallback type function to handle the result before returns eg. to throw an Error

Returns:

Returns Type Description
value is B boolean By default B variable is equal to the type detected from the value and the return type is a boolean as the result of its statement indicating the value is of AnyBoolean type detected from the value

The return value is a boolean indicating whether or not the value is a boolean type or Boolean instance.


guardClass

update

4.1.0: Fixes the value is not guarded by extending generic type Class variable with Function.

Use guardClass() or guard.is.class() to guard the value to be a generic Class type of class.

const guardClass: GuardClass = <Class extends Function>(
  value: Class,
  callback?: ResultCallback
): value is Class => isClass<Class>(value, callback);

Generic type variables:

Name Default value Description
Class extends Function From the value Guarded with Function, generic Class variable from the value to the return type value is Class

Parameters:

Name: type Description
value: Class A Function type value, by default of a generic Class type detected from the value to guard
callback?: ResultCallback An optional ResultCallback type function to handle the result before returns eg. to throw an Error

Returns:

Returns Type Description
value is Class boolean By default Class variable is equal to the type detected from the value and the return type is a boolean as the result of its statement indicating the value is a Function of type detected from the value

The return value is boolean indicating whether or not the value is a class.

Usage:

// Example usage.
import { guardClass } from '@angular-package/type';

type Func = (...param: any) => any;

/**
 * typeof === 'function'
 * instanceof Function === true
 * instanceof Object === true
 */
const FUNCTION: Func = (x: number, y: string): any => x + y;

/**
 * typeof === 'function'
 * instanceof Class === false
 * instanceof Function === true
 * instanceof Object === true
 */
class Class {

  1030405027 = 'my new number';
  5 = 'my number';

  firstName = 'My name';
  surname = 'Surname';

  x = NUMBER;
  y = STRING;

  get [number](): number {
    return this.x;
  }
  get [STRING](): string {
    return this.y;
  }

  get [SYMBOL_NUMBER](): number {
    return this.x;
  }

  get [SYMBOL_STRING](): string {
    return this.y;
  }
}

guardClass(FUNCTION); // false
guardClass<Class>(FUNCTION); // type error

guardDefined

update

4.1.0: Fixes the value is not guarded by changing its type to Defined<Type>.

Use guardDefined() or guard.is.defined() to guard the value to be defined.

const guardDefined: GuardDefined = <Type>(
  value: Defined<Type>,
  callback?: ResultCallback
): value is Defined<Type> => isDefined(value, callback);

Generic type variables:

Name Default value Description
Type From the value Guarded with Defined, a generic Type variable from the value to the return type value is Defined<Type>

Parameters:

Name: type Description
value: Defined<Type> A generic type value, by default of not undefined type detected from the value to guard against undefined
callback?: ResultCallback An optional ResultCallback type function to handle the result before returns eg. to throw an Error

Returns:

Returns Type Description
value is Defined<Type> boolean By default Type variable is equal to the type detected from the value, but the detected type undefined changes to never and the return type is a boolean as the result of its statement indicating the value is of type detected from the value

The return value is a boolean indicating whether or not the value is defined.

Usage:

// Example usage.
import { guardDefined } from '@angular-package/type';

let letFirstName = 'my name';
guardDefined(letFirstName); // true; return type `value is string`

const firstName = 'my const name';
guardDefined(firstName); // true; return type `value is string`

guardFunction

Use guardFunction() or guard.is.function() to guard the value to be a Func type.

const guardFunction: GuardFunction = (
  value: Func,
  callback?: ResultCallback
): value is Func => isFunction(value, callback);

Parameters:

Name: type Description
value: Func A Func type value to guard
callback?: ResultCallback An optional ResultCallback type function to handle the result before returns eg. to throw an Error

Returns:

Returns Type Description
value is Func boolean The return type is a boolean as the result of its statement indicating the value is Func

The return value is a boolean indicating whether or not the value is a Func.

Usage:

Example usage on playground


guardInstance

update

4.1.0: Fixes the value is not guarded by extending generic type Obj variable with object.

4.1.1: Changes the instance parameter name to constructor cause it represents class or function constructor.

Use guardInstance() or guard.is.instance() to guard the value to be an object of a generic Obj type and an instance of Constructor type.

const guardInstance: GuardInstance = <Obj extends object>(
  value: Obj,
  constructor: Constructor<Obj>,
  callback?: ResultCallback
): value is Obj => isInstance<Obj>(value, constructor, callback);

Generic type variables:

Name Default value Description
Obj extends object From the value Guarded with object, Obj variable from the value to the return type value is Obj

Parameters:

Name: type Description
value: Obj An object, by default of a generic Obj type detected from the value to guard and to check if it's a constructor instance
constructor: Constructor<Obj> A class or function that specifies the type of the constructor
callback?: ResultCallback An optional ResultCallback type function to handle the result before returns eg. to throw an Error

Returns:

Returns Type Description
value is Obj boolean By default Obj variable is equal to the type detected from the value and the return type is a boolean as the result of its statement indicating the value is an object of type detected from the value

The return value is a boolean indicating whether or not the value is an instance of a generic Obj.

Usage:

// Usage example.
import { guardInstance } from '@angular-package/type';

// Person interface.
interface Person {
  firstName?: string;
  surname?: string;
  age?: number;
  sex?: 'male' | 'female';
}

// Class constructor.
class Persons implements Person {
  firstName = '';
  surname = '';
  age = 15;
}

// Function person constructor.
function personFunctionConstructor(this: Person, ...args: any[]): Person {
  if (args) {
    this.firstName = args[0];
    this.surname = args[1];
    this.age = args[2];
    this.sex = args[3];
  }
  return this;
}

const personInstance: Person = new (personFunctionConstructor as any)('First name', 'Sur name', 27);
const personsInstance: Persons = new Persons();

guardInstance(personInstance, personFunctionConstructor as any); // true
guardInstance(personsInstance, Persons); // true

guardKey

update

4.1.0: Fixes the return type boolean to value is Key.

Use guardKey() or guard.is.key() to guard the value to be one of the string, number, or symbol.

const guardKey: GuardKey = (
  value: Key,
  callback?: ResultCallback
): value is Key => isKey(value, callback);

Parameters:

Name: type Description
value: Key A Key type value to guard
callback?: ResultCallback An optional ResultCallback type function to handle the result before returns eg. to throw an Error

Returns:

Returns Type Description
value is Key boolean The return type is a boolean as the result of its statement indicating the value is Key

The return value is a boolean indicating whether or not the value is a Key.


guardNull

Use guardNull() or guard.is.null() to guard the value to be a null.

const guardNull: GuardNull = (
  value: null,
  callback?: ResultCallback
): value is null => isNull(value, callback);

Parameters:

Name: type Description
value: null A null type value to guard
callback?: ResultCallback An optional ResultCallback type function to handle the result before returns eg. to throw an Error

Returns:

Returns Type Description
value is null boolean The return type is a boolean as the result of its statement indicating the value is a null

The return value is a boolean indicating whether or not the value is a null.


guardNumber

Use guardNumber() or guard.is.number() to guard the value to be any type of a number.

const guardNumber: GuardNumber = <N extends AnyNumber>(
  value: N,
  callback?: ResultCallback
): value is N => isNumber(value, callback);

Generic type variables:

Name Default value Description
N extends AnyNumber From the value Guarded with AnyNumber, N variable from the value to the return type value is N

Parameters:

Name: type Description
value: N An AnyNumber type value, by default of a generic N type detected from the value to guard
callback?: ResultCallback An optional ResultCallback type function to handle the result before returns eg. to throw an Error

Returns:

Returns Type Description
value is N boolean By default N variable is equal to the type detected from the value and the return type is a boolean as the result of its statement indicating the value is of AnyNumber type detected from the value

The return value is a boolean indicating whether or not the value is a number type or Number object.

Usage:

Example usage on playground


guardObject

update

4.1.0: Fixes the value is not guarded by extending generic type Obj variable with object.

Use guardObject() or guard.is.object() to guard the value to be an object of a generic Obj type.

const guardObject: GuardObject = <Obj extends object>(
  value: Obj,
  callback?: ResultCallback
): value is Obj => isObject<Obj>(value, callback);

Generic type variables:

Name Default value Description
Obj extends object From the value Guarded with object, Obj variable from the value to the return type value is Obj

Parameters:

Name: type Description
value: Obj An object of a generic Obj type, by default of type detected from the value to guard
callback?: ResultCallback An optional ResultCallback type function to handle the result before returns eg. to throw an Error

Returns:

Returns Type Description
value is Obj boolean By default Obj variable is equal to the type detected from the value and the return type is a boolean as the result of its statement indicating the value is an object of type detected from the value

The return value is a boolean indicating whether or not the value is an object.

Usage:

Example usage on playground


guardObjectKey

update

4.1.0: Fixes the value is not guarded by extending generic type Obj variable with object.

Use guardObjectKey() or guard.is.objectKey() to guard the value to be an object of a generic Obj type that contains the key.

const guardObjectKey: GuardObjectKey = <Obj extends object>(
  value: Obj,
  key: keyof Obj | (keyof Obj)[],
  callback?: ResultCallback
): value is Obj => isObjectKey<Obj>(value, key, callback);

Generic type variables:

Name Default value Description
Obj extends object From the value Guarded with object, Obj variable from the value to the return type value is Obj

Parameters:

Name: type Description
value: Obj An object of a generic Obj type that contains the key, by default of type detected from the value to guard
key: keyof Obj | (keyof Obj)[] A key of Obj or an array of keys of Obj type as the name of the property that the value contains
callback?: ResultCallback An optional ResultCallback type to handle the result before returns eg. to throw an Error

Returns:

Returns Type Description
value is Obj boolean By default Obj variable is equal to the type detected from the value and the return type is a boolean as the result of its statement indicating the value is an object of type detected from the value

The return value is a boolean indicating whether or not the value is an object of a generic Obj containing the key.

Usage:

Example usage on playground


guardObjectKeys

new

Use guardObjectKeys() or guard.is.objectKeys() to guard the value to be an object of a generic Type with some of its own specified keys. The function uses isObjectKeys() to search for the keys and it means:

Cause of using some() on the rest parameter ...keys each of its argument is treated as logic or, and cause of using every() on its array argument each of array item is treated as logic and. Simply, in the usage section below the function finds in the object get and set or writable and value, which means the object contains get and set or writable and value. The function uses hasOwnProperty Object method to finds enumerable and non-enumerable Key as string, number, symbol unlike Object.keys() but it can't find accessor descriptor property unlike in operator, which can.

const guardObjectKeys: GuardObjectKeys = <Obj extends object>(
  value: Obj,
  ...keys: (keyof Obj | Array<keyof Obj>)[]
): value is Obj => isObjectKeys<Obj>(value, ...keys);

Generic type variables:

Name Default value Description
Obj extends object From the value Guarded with object, Obj variable from the value to the return type value is Obj

Parameters:

Name: type Description
value: Obj An object of a generic Obj type that contains the keys, by default of type detected from the value to guard
...keys: keyof Obj | (keyof Obj)[] A rest parameter single key of Obj or an array of key of Obj type as the name of the property that the value contains

Returns:

Returns Type Description
value is Obj boolean By default Obj variable is equal to the type detected from the value and the return type is a boolean as the result of its statement indicating the value is an object of type detected from the value

The return value is a boolean indicating whether or not the value is an object with some of its own specified keys.

Usage:

// Real example usage from incoming @angular-package/property.
interface CommonDescriptor extends Pick<PropertyDescriptor, 'configurable' | 'enumerable'> {}
interface AccessorDescriptor<Value> extends CommonDescriptor {
  get: (() => Value) | undefined;
  set: ((value: Value) => void) | undefined;
}
interface DataDescriptor<Value> extends CommonDescriptor {
  writable: boolean;
  value: Value;
}
type ThisAccessorDescriptor<Value, Obj = any> = AccessorDescriptor<Value> & ThisType<Obj>;

const ACCESSOR_DESCRIPTOR: ThisAccessorDescriptor<string | undefined, ObjectOne> =  {
  configurable: true,
  enumerable: true,
  get(): string | undefined {
    return this[5];
  },
  set(value: string | undefined) {
    this[5] = value;
  }
}

const DATA_DESCRIPTOR: DataDescriptor<string> = {
  configurable: true,
  enumerable: true,
  writable: true,
  value: 'my value'
}

guardObjectKeys(DATA_DESCRIPTOR, ['writable', 'value'], ['get', 'set']); // type error on ['get', 'set']
guardObjectKeys(DATA_DESCRIPTOR, ['writable', 'value']); // true
guardObjectKeys(DATA_DESCRIPTOR, 'writable', 'value'); // true
guardObjectKeys(DATA_DESCRIPTOR, ['get', 'set']); // type error
guardObjectKeys(DATA_DESCRIPTOR, 'get', 'set'); // type error first on 'get' then on 'set'

guardObjectKeys(ACCESSOR_DESCRIPTOR, ['writable', 'value'], ['get', 'set']); // type error on ['writable', 'value']
guardObjectKeys(ACCESSOR_DESCRIPTOR, ['configurable', 'enumerable'], 'writable', 'value'); // type error on 'writable'
guardObjectKeys(ACCESSOR_DESCRIPTOR, ['configurable', 'enumerable'], 'get'); // true
guardObjectKeys(ACCESSOR_DESCRIPTOR, ['configurable', 'enumerable'], 'set'); // true

guardPrimitive

Use guardPrimitive() or guard.is.primitive() to guard the value to be the Primitive from a type of the Primitives.

const guardPrimitive: GuardPrimitive = <Type extends Primitive>(
  value: Type,
  type: Primitives,
  callback?: ResultCallback
): value is Type => isPrimitive<Type>(value, type, callback);

Generic type variables:

Name Default value Description
Type extends Primitive From the value Guarded with Primitive type, Type variable from the value to the return type value is Type

Parameters:

Name: type Description
value: Type A Primitive type value, by default of a generic Type detected from the value to guard
type: Primitives A string type from the Primitives to check the value
callback?: ResultCallback An optional ResultCallback type function to handle the result before returns eg. to throw an Error

Returns:

Returns Type Description
value is Type boolean By default Type variable is equal to the type detected from the value and the return type is a boolean as the result of its statement indicating the value is of Primitive type detected from the value

The return value is a boolean indicating whether or not the value is the Primitive from the type.

Usage:

// Example usage.
import { guardPrimitive } from '@angular-package/type';

// string.
const firstName = 'firstName';
guardPrimitive(firstName, 'string'); // true; return type `value is "firstName"`
let letFirstName = 'firstName';
guardPrimitive(letFirstName, 'string'); // true; return type `value is string`

// number.
const age = 5;
guardPrimitive(age, 'number'); // true; return type `value is 5`
let letAge = 5;
guardPrimitive(letAge, 'number'); // true; return type `value is number`

// null.
const myNull = null;
guardPrimitive(myNull, 'null'); // true; return type `value is null`

// bigint
const oldAge = 1n;
guardPrimitive(oldAge, 'bigint'); // true; return type `value is 1n`
let letOldAge = 1n;
guardPrimitive(letOldAge, 'bigint'); // true; return type `value is bigint`

// Boolean.
const question = true;
guardPrimitive(question, 'boolean'); // true; return type `value is true`
let letQuestion = true;
guardPrimitive(letQuestion, 'boolean'); // true; return type `value is true`

// Undefined.
const und = undefined;
guardPrimitive(und, 'undefined'); // true; return type `value is undefined`
let letUndefined ;
guardPrimitive(letUndefined, 'undefined'); // true; return type `value is undefined`

// Symbol.
guardPrimitive(Symbol(firstName), 'symbol'); // true; return type `value is symbol`

Example usage on playground


guardString

Use guardString() or guard.is.string() to guard the value to be string of any type.

const guardString: GuardString = <S extends AnyString>(
  value: S,
  callback?: ResultCallback
): value is S => isString(value, callback);

Generic type variables:

Name Default value Description
S extends AnyString From the value Guarded with AnyString type, S variable from the value to the return type value is S

Parameters:

Name: type Description
value: S An AnyString type value, by default of a generic S type detected from the value to guard
callback?: ResultCallback An optional ResultCallback type to handle the result before returns eg. to throw an Error

Returns:

Returns Type Description
value is S boolean By default S variable is equal to the type detected from the value and the return type is a boolean as the result of its statement indicating the value is of AnyString type detected from the value

The return value is a boolean indicating whether or not the value is a string type or String instance.

Usage:

// Example usage.
import { guardString } from '@angular-package/type';

let letFirstName = 'not my name';
guardString(letFirstName); // true; return type `value is string`

const firstName = 'my name';
guardString(firstName); // true; return type `value is "my name"`

Example usage on playground


guardSymbol

Use guardSymbol() or guard.is.symbol() to guard the value to be a symbol.

const guardSymbol: GuardSymbol = (
  value: symbol,
  callback?: ResultCallback
): value is symbol => isSymbol(value, callback);

Parameters:

Name: type Description
value: symbol A symbol type value to guard
callback?: ResultCallback An optional ResultCallback type function to handle the result before returns eg. to throw an Error

Returns:

Returns Type Description
value is symbol boolean The return type is a boolean as the result of its statement indicating the value is a symbol

The return value is a boolean indicating whether or not the value is a symbol.

Usage:

// Example usage.
import { guardSymbol } from '@angular-package/type';

const SYMBOL_NUMBER: unique symbol = Symbol(27);
const SYMBOL_STRING: unique symbol = Symbol('Twenty seven');

guardSymbol(SYMBOL_NUMBER); // true; return type `value is symbol`
guardSymbol(SYMBOL_STRING); // true; return type `value is symbol`

guardType

Use guardType() or guard.is.type() to guard the value to be the Type from a type of the Types.

const guardType: GuardType = <T extends Type>(
  value: T,
  type: Types<T>,
  callback?: ResultCallback
): value is T => isType<T>(value, type, callback);

Generic type variables:

Name Default value Description
T extends Type From the value Guarded with Type, T variable from the value to the return type value is T

Parameters:

Name: type Description
value: T A Type value, by default of a generic T type detected from the value to guard with the type
type: Types<T> A string or generic Constructor<T> type from the Types to check the value
callback?: ResultCallback An optional ResultCallback type function to handle the result before returns eg. to throw an Error

Returns:

Returns Type Description
value is T boolean By default T variable is equal to the type detected from the value and the return type is a boolean as the result of its statement indicating the value is of type detected from the value

The return value is a boolean indicating whether or not the value is a type from the Types.

Usage:

// Example usage.
import { guardType } from '@angular-package/type';

// Person interface.
interface PersonData { firstName: string; }

// Class.
class Person {}

// Object.
const someone: Person = new Person();

const object: PersonData = { firstName: 'firstName' };
guardType(object, 'object'); // true; return type `value is PersonData`

let letObject: PersonData = { firstName: 'firstName' };
guardType(letObject, 'object'); // true; return type `value is PersonData`

function myPerson(person: Person): Person { return person; }
guardType(myPerson, 'function'); // true; return type `value is (person: Person) => Person`

// string.
const firstName = 'firstName';
guardType(firstName, 'string'); // true; return type `value is "firstName"`
let letFirstName = 'firstName';
guardType(letFirstName, 'string'); // true; return type `value is string`

// number.
const age = 5;
guardType(age, 'number'); // true; return type `value is 5`
let letAge = 5;
guardType(letAge, 'number'); // true; return type `value is number`

// null.
const myNull = null;
guardType(myNull, 'null'); // true; return type `value is null`

// bigint
const oldAge = 1n;
guardType(oldAge, 'bigint'); // true; return type `value is 1n`
let letOldAge = 1n;
guardType(letOldAge, 'bigint'); // true; return type `value is bigint`

// Boolean.
const question = true;
guardType(question, 'boolean'); // true; return type `value is true`
let letQuestion = true;
guardType(letQuestion, 'boolean'); // true; return type `value is true`

// Undefined.
const und = undefined;
guardType(und, 'undefined'); // true; return type `value is undefined`
let letUndefined ;
guardType(letUndefined, 'undefined'); // true; return type `value is undefined`

// Symbol.
guardType(Symbol(firstName), 'symbol'); // true; return type `value is symbol`

// Instance.
guardType(someone, Person); // true; return type `value is Person`

Example usage on playground


guardUndefined

Use guardUndefined() or guard.is.undefined() to guard the value to be undefined.

const guardUndefined: GuardUndefined = (
  value: undefined,
  callback?: ResultCallback
): value is undefined => isUndefined(value, callback);

Parameters:

Name: type Description
value: undefined An undefined type value to guard
callback?: ResultCallback An optional ResultCallback type function to handle the result before returns eg. to throw an Error

Returns:

Returns Type Description
value is undefined boolean The return type is a boolean as the result of its statement

The return value is a boolean indicating whether or not the value is undefined.

Usage:

// Example usage.
import { guardUndefined } from '@angular-package/type';

const UNDEFINED: undefined = undefined;

guardUndefined(UNDEFINED); // true; 
guardUndefined('x'); // false; type error
guardUndefined(5); // false; type error

Common types

AnyBoolean

Represents boolean type or Boolean object.

type AnyBoolean = Exclude<boolean | Boolean, true | false>;

AnyNumber

Represents number type or Number object.

type AnyNumber = number | Number;

AnyString

Represents string type or String object.

type AnyString = string | String;

Constructor

type Constructor<Type> = new (...args: any[]) => Type;

Defined

Indicate that Type variable is defined.

type Defined<Type> = Never<undefined, Type>;

CycleHook

Angular cycle hooks method names.

type CycleHook =
  | 'ngAfterContentInit'
  | 'ngAfterContentChecked'
  | 'ngAfterViewInit'
  | 'ngAfterViewChecked'
  | 'ngAfterViewChecked'
  | 'ngOnInit'
  | 'ngOnDestroy'
  | 'ngOnChanges';

Func

Function type.

type Func = (...param: any) => any;

Key

Name of the object property.

type Key =  number | string | symbol;

Never

new

Choose a type to exclude. A generic Type is never a Not type.

type Never<Not, Type> = Type extends Not ? never : Type;

Primitive

All Primitive types.

type Primitive = bigint | boolean | null | number | string | symbol | undefined;

Primitives

All Primitive types as string.

type Primitives = 'bigint' | 'boolean' | 'null' | 'number' | 'symbol' | 'string' | 'undefined';

ResultCallback

type ResultCallback = <Obj>(result: boolean, value?: any) => boolean;

Type

Main types.

type Type = Func | object | Primitive;

Types

Main types as string.

type Types<Obj> = Constructor<Obj> | 'function' | 'object' | Primitives;

Undefined

new

Undefined or never - treat types as never excluding undefined.

type Undefined<Type> = Type extends undefined ? Type : never;

Experimental

BigIntObject

The object handles creating and getting the BigInt with BigInt().

class BigIntObject {
  static set set(value: any) {
    PrimitiveObject.bigint = BigInt(value);
  }
  static get get(): BigInt {
    return PrimitiveObject.bigint;
  }
}

Create a new BigInt by assign value to the set property.

BigIntObject.set = 1n;

Get created BigInt with the get property.

const bigint: BigIntObject = BigIntObject.get;

BooleanObject

The object handles creating and getting the Boolean object instance with Boolean().

class BooleanObject {
  /**
   * `false` when empty, 0, null, '', false
   * `true` when 'true', 'false', 'Su Lin whatever', [], {}, true
   */
  static set set(value: any) {
    PrimitiveObject.boolean = new Boolean(value);
  }
  static get get(): Boolean {
    return PrimitiveObject.boolean;
  }
}

Create a new Boolean instance by assign value to the set property.

BooleanObject.set = false;

Get created Boolean instance with the get property.

const booleanInstance: Boolean = BooleanObject.get;

NumberObject

The object handles creating and getting the Number object instance with Number().

class NumberObject {
  static set set(value: any) {
    PrimitiveObject.number = new Number(value);
  }
  static get get(): Number {
    return PrimitiveObject.number;
  }
}

Create a new Number instance by assign value to the set property.

NumberObject.set = 'my number instance';

Get created Number instance with the get property.

const numberInstance: Number = NumberObject.get;

PrimitiveObject

The object to store static primitive objects.

class PrimitiveObject  {
  static bigint: BigInt;
  static boolean: Boolean;
  static number: Number;
  static string: String;
  static symbol: Symbol;
}

StringObject

The object handles creating and getting the String object instance with String().

class StringObject {
  static set set(value: any) {
    PrimitiveObject.string = new String(value);
  }
  static get get(): String {
    return PrimitiveObject.string;
  }
}

Create a new String instance by assign value to the set property.

StringObject.set = 'my string instance';

Get created String instance with the get property.

const stringInstance: String = StringObject.get;

SymbolObject

The object handles creating and getting the Symbol object instance with Symbol().

class SymbolObject {
  static set set(value: string | number | undefined) {
    PrimitiveObject.symbol = Symbol(value);
  }
  static get get(): Symbol {
    return PrimitiveObject.symbol;
  }
}

Create a new symbol by assigning value to the set property.

SymbolObject.set = 'my symbol';

Get created symbol with the get property.

const symbol: Symbol = SymbolObject.get;

isParam

Method decorator to check the type and return undefined if it's not the same as expected.

function isParam(...param: Array<string>): MethodDecorator {
  return (target: Func | object, key: string | symbol, descriptor: any): any => {
    const originalMethod = descriptor.value;

    descriptor.value =  function(): void {
      if (is.array(param) && is.defined(arguments)) {
        param.forEach((name: string, index: number) => {
          if (is.number(index) && index < arguments.length) {
            if (is.defined(arguments[index])) {
              switch (name) {
                case 'number':
                  if (is.number(arguments[index]) === false) {
                    arguments[index] = undefined;
                  }
                  break;
                case 'object':
                  if (is.object(arguments[index]) === false) {
                    arguments[index] = undefined;
                  }
                  break;
                case 'string':
                  if (is.string(arguments[index]) === false) {
                    arguments[index] = undefined;
                  }
                  break;
              }
            }
          }
        });
      }
      const result = originalMethod.apply(this, arguments);
      return result;
    };

    return descriptor;
  };
}

Example usage.

// Example  usage
import { isParam } from '@angular-package/type';

const STRING: any = '!@#$%^&*()abcdefghijklmnoprstuwyz';
const NUMBER: any = 10304050;

// TestClass
class TestClass {
  @isParam('object', 'string', 'number')
  public testMethod(object?: any, firstName?: any, age?: any): { object: any, firstName: any, age: any } {
    return {object, firstName, age};
  }
}
const resultTRUE = new TestClass().testMethod({firstName: 'NoName'}, STRING, NUMBER);
const resultFALSE = new TestClass().testMethod(NUMBER, {firstName: 'NoName'}, STRING);

resultTRUE === {
  object: {firstName: 'NoName'},
  string: '!@#$%^&*()abcdefghijklmnoprstuwyz',
  number: 10304050
};

resultTRUE === {
  object: undefined,
  string: undefined,
  number: undefined
};

GIT

Commit

Versioning

Semantic Versioning 2.0.0

Given a version number MAJOR.MINOR.PATCH, increment the:

  • MAJOR version when you make incompatible API changes,
  • MINOR version when you add functionality in a backwards-compatible manner, and
  • PATCH version when you make backwards-compatible bug fixes.

Additional labels for pre-release and build metadata are available as extensions to the MAJOR.MINOR.PATCH format.

FAQ How should I deal with revisions in the 0.y.z initial development phase?

The simplest thing to do is start your initial development release at 0.1.0 and then increment the minor version for each subsequent release.

How do I know when to release 1.0.0?

If your software is being used in production, it should probably already be 1.0.0. If you have a stable API on which users have come to depend, you should be 1.0.0. If you’re worrying a lot about backwards compatibility, you should probably already be 1.0.0.

License

MIT © angular-package (license)