Rush StackShopBlogEvents
Skip to main content

Home > @rushstack/node-core-library > TypeUuid

TypeUuid class

Provides a version-independent implementation of the JavaScript instanceof operator.

Signature:

export declare class TypeUuid 

Remarks

The JavaScript instanceof operator normally only identifies objects from a particular library instance. For example, suppose the NPM package example-lib has two published versions 1.2.0 and 1.3.0, and it exports a class called A. Suppose some code consumes version 1.3.0 of the library, but it receives an object that was constructed using version 1.2.0. In this situation a instanceof A will return false, even though a is an instance of A. The reason is that there are two prototypes for A; one for each version.

The TypeUuid facility provides a way to make a instanceof A return true for both prototypes of A, by instead using a universally unique identifier (UUID) to detect object instances.

You can use Symbol.hasInstance to enable the system instanceof operator to recognize type UUID equivalence:

const uuidWidget: string = '9c340ef0-d29f-4e2e-a09f-42bacc59024b';
class Widget {
public static [Symbol.hasInstance](instance: object): boolean {
return TypeUuid.isInstanceOf(instance, uuidWidget);
}
}

// Example usage:

import { Widget as Widget1 } from 'v1-of-library';
import { Widget as Widget2 } from 'v2-of-library';
const widget = new Widget2();
console.log(widget instanceof Widget1); // prints true

Methods

MethodModifiersDescription
isInstanceOf(targetObject, typeUuid)staticReturns true if the targetObject is an instance of a JavaScript class that was previously registered using the specified typeUuid. Base classes are also considered.
registerClass(targetClass, typeUuid)staticRegisters a JavaScript class as having a type identified by the specified UUID.