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
  • guardType()
  • Generic type variables
  • Parameters
  • Return type
  • Returns
  • Example usage

Was this helpful?

Edit on GitHub
  1. Guard

guardType()

Previous★ guardTrue()NextguardUndefined()

Last updated 3 years ago

Was this helpful?

guardType()

Guards the value to be a type of given .

guard-type.func.ts
const guardType = <T extends Type, Payload extends object = object>(
  value: T,
  type: Types<T>,
  callback?: ResultCallback<T, Payload>,
  payload?: Payload
): value is T => isType(value, type, callback, payload);

Generic type variables

TextendsType

Payloadextendsobject=object

Parameters

value: T

type: Types<T>

callback?: ResultCallback<T, Payload>

payload?: Payload

Return type

value isT

Returns

Example 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`

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

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 a generic type variable constrained by generic type , by default of the type captured from itself, to guard.

The value of or type of the indicates against which type a given is checked.

The optional callback of type with parameters, the that has been checked, the result of this check, and payload of generic type variable with optional properties from the provided payload, to handle them before the result return. By default, it uses resultCallback() 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 of the type captured from the supplied .

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

Type
T
string
Constructor<T>
Types
value
function
ResultCallback
value
Payload
boolean
value
T
value
boolean
Types
value
type
Type
ResultCallback
value
return type
callback
object
callback
payload
guardType()
object
Payload
callback
type
value
payload
payload
Logotype/guard-type.func.ts at main · angular-package/typeGitHub