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

Was this helpful?

Edit on GitHub
  1. is

isPrimitive()

PreviousisObjectSomeKeys()NextisRegExp()

Last updated 3 years ago

Was this helpful?

isPrimitive()

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

is-primitive.func.ts
const isPrimitive = <
  Type extends Primitive,
  Payload extends object = object
>(
  value: any,
  type?: Primitives,
  callback: ResultCallback<any, Payload> = resultCallback,
  payload?: Payload
): value is Type =>
  isStringType(type)
    ? {
        bigint: isBigInt,
        boolean: isBooleanType,
        number: isNumberType,
        null: isNull,
        string: isStringType,
        symbol: isSymbol,
        undefined: isUndefined,
      }[type](value, callback, payload)
    : callback(
        isNull(value) ||
          (typeof value !== 'object' && typeof value !== 'function'),
        value,
        payload
      );

Generic type variables

TypeextendsPrimitive

Payloadextendsobject=object

Parameters

value: any

type?: Primitives

callback: ResultCallback<any, Payload>

payload?: Payload

Return type

value is Type

Returns

Example usage

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

isPrimitive(1n); // Returns `true` as `value is Primitive`
isPrimitive<bigint>(1n, 'bigint'); // Returns `true` as `value is bigint`
isPrimitive<boolean>(false, 'boolean'); // Returns `true` as `value is boolean`
isPrimitive<number>(27, 'number'); // Returns `true` as `value is number`
isPrimitive<null>(null, 'null'); // Returns `true` as `value is null`
isPrimitive<string>('age', 'string'); // Returns `true` as `value is string`
isPrimitive<symbol>(Symbol(27), 'symbol'); // Returns `true` as `value is symbol`
isPrimitive<undefined>(undefined, 'undefined'); // Returns `true` as `value is undefined`

A generic type variable Type constrained by the generic type type indicates the type of parameter via the value is 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 type to check.

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

A callback function of type with parameters, the 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 return value is a indicating whether the provided is a primitive type of the .

any
any
Primitive
Primitives
type
Primitive
value
return type
Primitives
value
boolean
Primitive
value
Type
boolean
Primitives
value
object
callback
payload
isPrimitive()
ResultCallback
resultCallback()
value
Payload
payload
object
Payload
callback
payload
result
payload
result
payload
Logotype/is-primitive.func.ts at main · angular-package/typeGitHub