Type
TwitterGitHub
Draft
Draft
  • Introduction
  • ❤ Benefits
  • Getting started
    • Skeleton
    • Installation
      • npm
    • Public API
    • General concepts
  • Helper
    • recognizeValue()
      • Recognized instances
      • OfRecognized
    • resultCallback()
    • typeOf()
  • is
    • isArray()
    • isBigInt()
    • isBoolean()
    • isBooleanObject()
    • isBooleanType()
    • isClass()
    • isDate()
    • isDefined()
    • ★ isFalse()
    • isFunction()
    • isInstance()
    • isKey()
    • isNull()
    • ★ isNumber()
    • isNumberBetween()
    • isNumberObject()
    • isNumberType()
    • isObject()
    • isObjectKey()
    • isObjectKeyIn()
    • isObjectKeys()
    • isObjectKeysIn()
    • isObjectSomeKeys()
    • isPrimitive()
    • isRegExp()
    • ★ isString()
    • isStringIncludes()
    • isStringIncludesSome()
    • isStringLength()
    • isStringLengthBetween()
    • isStringObject()
    • isStringType()
    • isSymbol()
    • ★ isTrue()
    • isType()
    • isUndefined()
  • is not
    • isNotBoolean()
    • isNotDefined()
    • isNotFunction()
    • isNotNull()
    • isNotNumber()
    • isNotString()
    • isNotUndefined()
  • Are
    • areDeterminer()
      • every()
      • forEach()
      • some()
    • areBigInt()
      • every()
      • forEach()
      • some()
    • areBoolean()
      • every()
      • forEach()
      • some()
    • areDate()
      • every()
      • forEach()
      • some()
    • areDefined()
      • every()
      • forEach()
      • some()
    • ★ areFalse()
      • every()
      • forEach()
      • some()
    • areNull()
      • every()
      • forEach()
      • some()
    • ★ areNumber()
      • every()
      • forEach()
      • some()
    • areRegExp()
      • every()
      • forEach()
      • some()
    • ★ areString()
      • every()
      • forEach()
      • some()
    • areSymbol()
      • every()
      • forEach()
      • some()
    • ★ areTrue()
      • every()
      • forEach()
      • some()
    • areUndefined()
      • every()
      • forEach()
      • some()
  • Guard
    • guardArray()
    • guardBigInt()
    • guardBoolean()
    • guardClass()
    • guardDate()
    • guardDefined()
    • ★ guardFalse()
    • guardFunction()
    • guardInstance()
    • guardKey()
    • guardNull()
    • guardNumber()
    • guardNumberBetween()
    • guardObject()
    • guardObjectKey()
    • guardObjectKeyIn()
    • guardObjectKeys()
    • guardObjectKeysIn()
    • guardObjectSomeKeys()
    • guardPrimitive()
    • guardRegExp()
    • ★ guardString()
    • guardStringIncludes()
    • guardStringIncludesSome()
    • guardStringLength()
    • guardStringLengthBetween()
    • guardSymbol()
    • ★ guardTrue()
    • guardType()
    • guardUndefined()
  • object
    • are: Are {}
    • guard: Guard {}
    • guardIs: GuardIs {}
    • is: Is {}
    • isNot: IsNot {}
    • type {}
  • Interface
    • MinMax
  • Type
    • AnyBoolean
    • AnyNumber
    • AnyString
    • CallbackPayload
    • Constructor
    • Defined
    • ForEachCallback
    • GenericObject
    • Never
    • NotUndefined
    • NumberBetween
    • Primitive
    • Primitives
    • ResultCallback
    • StringOfLength
    • Type
    • Types
    • Undefined
  • Experimental
    • isParam()
  • GIT
    • Commit
    • Semantic Versioning
  • Change log
    • Keep a changelog
    • Unreleased
    • v5.0.0-rc.0
    • v4.2.0
  • License
    • MIT
  • Contact
    • ⋯ Chat
    • @ Email
  • Donate
    • ฿ Cryptocurrency
    • $ Fiat
Powered by GitBook
On this page
  • isType()
  • Generic type variables
  • Parameters
  • Return type
  • Returns
  • Example usage

Was this helpful?

Edit on GitHub
  1. is

isType()

Previous★ isTrue()NextisUndefined()

Last updated 3 years ago

Was this helpful?

isType()

Checks if value is the type from a given of the generic type .

is-type.func.ts
const isType = <T extends Type, Payload extends object = object>(
  value: any,
  type: Types<T>,
  callback: ResultCallback<any, Payload> = resultCallback,
  payload?: Payload
): value is T =>
  isStringType(type)
    ? {
        bigint: isBigInt,
        boolean: isBooleanType,
        function: isFunction,
        number: isNumberType,
        object: isObject,
        null: isNull,
        string: isStringType,
        symbol: isSymbol,
        undefined: isUndefined,
      }[type as Primitives](value, callback, payload)
    : isNotNull(type)
    ? isInstance(value, type, callback, payload)
    : false;

Generic type variables

TextendsType

Payloadextendsobject=object

Parameters

value: any

type: Types<T>

callback: ResultCallback<any, Payload>

payload?: Payload

Return type

value is T

Returns

Example usage

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

class Person {}
const person = new Person();

// Primitives.
isType<bigint>(1n, 'bigint'); // Returns `true` as `value is bigint`
isType<boolean>(false, 'boolean'), // Returns `true` as `value is boolean`
isType<number>(27, 'number'); // Returns `true` as `value is number`
isType<null>(null, 'null'); // Returns `true` as `value is null`
isType<string>('age', 'string'); // Returns `true` as `value is string`
isType<symbol>(Symbol(27), 'symbol'); // Returns `true` as `value is symbol`
isType<undefined>(undefined, 'undefined'); // Returns `true` as `value is undefined`
// Function.
isType<Function>(() => {}, 'function'); // Returns `true` as `value is function`
// Object.
isType<object>({}, 'object'); // Returns `true` as `value is object`
// Class.
isType(person, Person); // Returns `true` as `value is Person`
// Class with callback and payload.
isType(person, Person, (result, value, payload) => {
  value // Person {}
  /*
  payload {
    value: {},
    id: 15,
    database: 'Persons'
  }
  */
  return result;
}, { id: 15, database: 'Persons' }); // Returns `true` as `value is Person`

A generic type variable T constrained by the generic type indicates the type of parameter via the value is T and captured type of the generic type of the supplied .

The Payload generic type variable constrained by indicates the type of optional parameter of the supplied function and optional parameter of the function from which it captures its value.

The value of type to check against the type of given .

A value of the generic type indicates against which type the provided is checked.

A callback function of type with parameters, the value that has been checked, the of this check, and of generic type variable with optional properties from the provided , to handle them before the return. By default, it uses function.

An optional of the generic type variable is assigned to the of the given function.

The return type is a as the result of its statement indicating the is a generic type variable by default equal to the but captured type of the supplied changes it to the class name.

The return value is a indicating whether the provided is a type from a given of the .

any
Types
type
Type
Constructor
value
return type
type
any
type
Types
value
boolean
Type
class
value
T
type
boolean
Types
value
type
object
callback
payload
isType()
ResultCallback
resultCallback()
Payload
payload
object
Payload
callback
payload
result
payload
result
payload
Logotype/is-type.func.ts at main · angular-package/typeGitHub