v2.0.0

Type

Runtime type guards — strings, numbers, plain objects, arrays, DOM nodes, Blobs, FormData, JSON-RPC message shapes.

Overview

Type is a singleton (new TypeManager()) exported from @bitrix24/b24jssdk. Every method (other than getTag and clone) is a TypeScript type guard, so you can use it in if chains and have the compiler narrow the value:

import { Type } from '@bitrix24/b24jssdk'

function process(value: unknown) {
  if (Type.isStringFilled(value)) {
    // value: string
    return value.trim()
  }
  if (Type.isPlainObject(value)) {
    // value: Record<string | number, any>
  }
}

The guards are grouped by the kind of value they check:

  • Strings — non-empty string checks.
  • Numbers / Booleans — number, integer, float, boolean.
  • Objects / Functions — plain objects, functions, Map / Set / WeakMap / WeakSet, RegExp, prototypes, null / undefined.
  • Arrays — arrays, array-like values, typed arrays, ArrayBuffer.
  • DOM — nodes, element nodes, text nodes.
  • FilesBlob, File, FormData.
  • JSON-RPC — request / response message shapes.
  • OthergetTag (raw [[Class]] tag) and clone (deep clone).

Method Signature

Strings

MethodPredicate
isString(value)typeof value === 'string' or value instanceof String.
isStringFilled(value)isString(value) && value !== ''.

Numbers / Booleans

MethodPredicate
isNumber(value)typeof value === 'number' and not NaN.
isInteger(value)Number.isInteger(value).
isFloat(value)isNumber && !isInteger.
isBoolean(value)value === true || value === false.

Objects / Functions

MethodPredicate
isFunction(value)typeof value === 'function' (also instances of Function).
isObject(value)typeof === 'object' || 'function' (and not null).
isObjectLike<T>(value)typeof === 'object' only (also true for null-safe non-null objects, including arrays).
isPlainObject(value)True for objects whose constructor is Object (false for arrays, class instances, DOM nodes).
isPrototype(value)True for prototype objects.
isMap, isSet, isWeakMap, isWeakSet, isRegExpTag-based checks (Object.prototype.toString.call(value)).
isDate(value)value instanceof Date.
isNil(value)value === null || value === undefined.
isNull / isUndefinedSelf-explanatory.

Arrays

MethodPredicate
isArray(value)Array.isArray.
isArrayFilled(value)isArray && value.length > 0.
isArrayLike(value)Has a numeric length and is not a function — also matches strings.
isArrayBuffer(value)Tag check.
isTypedArray(value)Matches Int8Array / Uint8Array / Uint8ClampedArray / Int16Array / Uint16Array / Int32Array / Uint32Array / Float32Array / Float64Array (tag check).

DOM

MethodPredicate
isDomNode(value)Has nodeType and is not a plain object.
isElementNode(value)nodeType === Node.ELEMENT_NODE.
isTextNode(value)nodeType === Node.TEXT_NODE.

Files / Blobs / FormData

MethodPredicate
isBlob(value)Has size: number, type: string, slice: Function.
isFile(value)isBlob && name: string && (lastModified || lastModifiedDate).
isFormData(value)instanceof FormData (when global available) or tag check.

JSON-RPC

MethodPredicate
isJsonRpcRequest(value)Has jsonrpc (non-empty) and method (non-empty). Return type is boolean, not a type guard.
isJsonRpcResponse(value)Has jsonrpc, id, and either result or error. Return type is boolean, not a type guard.

Other

getTag(value: any): string                          // Object.prototype.toString.call(value)
clone(obj: any, bCopyObj?: boolean): any             // deep clone; bCopyObj defaults to true

Key Concepts

  • isJsonRpcRequest / isJsonRpcResponse are plain boolean predicates, not type guards. Every other method in this class narrows with value is X; these two intentionally (or not) return boolean, so using them in an if does not narrow value's type.
  • isObjectLike<T> narrows to a caller-supplied generic, not to object. It only checks typeof value === 'object' && value !== null; the T you pass is not verified at runtime — arrays, Map, Date, and DOM nodes all pass. isPlainObject is the stricter check when you specifically want a {}-style object.
  • isArrayLike also matches strings. Strings have a numeric length and are not functions, so Type.isArrayLike('abc') is true. Use isArray when you need a real array.
  • isDomNode requires a real DOM global. isElementNode / isTextNode dereference the global Node constant, so they only work in a browser (or JSDOM) environment — calling them in plain Node.js without a DOM shim throws a ReferenceError.
  • clone mutates nothing in the source, but only clones plain data shapes it recognises. Arrays and plain objects are copied key-by-key (recursively unless bCopyObj is false), Date is rebuilt via new Date(obj), and DOM nodes go through Node.cloneNode(bCopyObj). Anything else with typeof obj === 'object' falls into the generic branch and is rebuilt via new obj.constructor(), so custom classes with required constructor arguments can throw.

Error Handling

The guard methods never throw for the value they inspect — a non-matching, null, or undefined value simply yields false. The exceptions are environment-dependent: isElementNode / isTextNode dereference the global Node constant and throw a ReferenceError outside a DOM environment, and clone can throw when it falls back to new obj.constructor() for a class that requires constructor arguments. Guard those calls when running server-side or when cloning arbitrary class instances.

Examples

Narrowing with string and object guards:

import { Type } from '@bitrix24/b24jssdk'

function describe(value: unknown): string {
  if (Type.isStringFilled(value)) {
    return `string: ${value.trim()}`
  }
  if (Type.isPlainObject(value)) {
    return `object with ${Object.keys(value).length} keys`
  }
  if (Type.isArrayFilled(value)) {
    return `array with ${value.length} items`
  }
  return 'unknown'
}

describe('  hi  ') // 'string: hi'
describe({ a: 1 }) // 'object with 1 keys'
describe([1, 2, 3]) // 'array with 3 items'

Tag-based checks and getTag:

import { Type } from '@bitrix24/b24jssdk'

Type.getTag([1, 2, 3])   // '[object Array]'
Type.getTag(new Map())   // '[object Map]'
Type.getTag(/abc/)       // '[object RegExp]'

Type.isMap(new Map())    // true
Type.isSet(new Set())    // true
Type.isRegExp(/abc/)     // true

JSON-RPC message shapes:

import { Type } from '@bitrix24/b24jssdk'

Type.isJsonRpcRequest({ jsonrpc: '2.0', method: 'ping', id: 1 }) // true
Type.isJsonRpcRequest({ jsonrpc: '2.0' })                         // false, no `method`

Type.isJsonRpcResponse({ jsonrpc: '2.0', id: 1, result: { ok: true } }) // true
Type.isJsonRpcResponse({ jsonrpc: '2.0', id: 1 })                       // false, no `result`/`error`

Deep cloning with clone:

import { Type } from '@bitrix24/b24jssdk'

const original = { a: 1, nested: { b: 2 } }

const deep = Type.clone(original)
deep.nested.b = 99
console.log(original.nested.b) // 2 — untouched, deep clone

const shallow = Type.clone(original, false)
shallow.nested.b = 42
console.log(original.nested.b) // 42 — same nested reference, shallow clone

Alternatives and Recommendations

  • Prefer the type-guard methods (value is X) over manual typeof / instanceof checks. They read the same but also narrow the value's TypeScript type inside the if branch, which the two boolean-returning JSON-RPC checks do not.
  • Use isPlainObject instead of isObject when you need a {}-style record. isObject also accepts functions, and isObjectLike accepts arrays, Map, Date, and DOM nodes — isPlainObject is the only one of the three that excludes all of those.
  • isTypedArray covers the nine concrete typed-array views, not DataView. It matches Int8ArrayFloat64Array but returns false for a DataView; use ArrayBuffer.isView(value) when you want to include DataView as well.
  • String/number coercion and formatting live in Text. Type only checks shapes; reach for Text.toNumber / Text.toInteger / Text.toBoolean when you need to convert a value rather than just test it.
  • clone is a general-purpose deep clone, not a structured-clone replacement. For values you control (plain data returned from the REST API) it is sufficient; for Map / Set / ArrayBuffer payloads or values that require exact prototype fidelity, prefer structuredClone (where available) or a dedicated cloning library.