Skip to content
1 min read · 140 words

Function: isInstanceOf()

ts
function isInstanceOf<T>(
  value: unknown,
  type: string,
  ctor?: (...args: any[]) => T,
): value is T;

Returns true if value is an instance of the class identified by type (and optionally ctor).

Type Parameters

Type ParameterDescription
TThe expected instance type.

Parameters

ParameterTypeDescription
valueunknownThe value to test.
typestringThe constructor name to compare against when instanceof is unavailable.
ctor?(...args: any[]) => TOptional constructor to use for instanceof and Symbol.hasInstance checks.

Returns

value is T

true when value is an instance of the class described by type/ctor.

Remarks

Performs three checks in order: instanceof ctor, Symbol.hasInstance, then constructor-name comparison. The constructor-name fallback handles cross-realm cases where instanceof fails.