Complete type checking utility library for Node.js with TypeScript support and type guards
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
Type checking for objects from external libraries. Currently supports Long objects from the 'long' npm package.
Returns true if value is a LongObject from the 'long' npm package.
/**
* Returns true if val is LongObject
* LongObject is from npm package `long`
* @param obj - Value to check
* @returns Type guard indicating if value is LongObject
*/
function isLongObject(obj?: unknown): obj is LongObject;
interface LongObject {
high: number;
low: number;
}Usage Example:
import { isLongObject } from "is-type-of";
import Long from "long"; // npm install long
const longValue = Long.fromNumber(123456789);
const regularObject = { high: 1, low: 2 };
isLongObject(longValue); // => true
isLongObject(regularObject); // => true (if it has high/low number properties)
isLongObject({ high: "1", low: 2 }); // => false (high must be number)
isLongObject({}); // => falseThe long.js library provides support for 64-bit integers in JavaScript. Long objects have the following structure:
interface LongObject {
high: number; // The high 32 bits
low: number; // The low 32 bits
}The isLongObject function validates that an object:
high and low propertiesNote: This function checks for the structural shape of Long objects, not strict instanceof checking. Any object with the correct high and low number properties will pass this check.
import { isLongObject } from "is-type-of";
import Long from "long";
function processLongValue(value: unknown) {
if (isLongObject(value)) {
// value is now typed as LongObject
console.log(`High: ${value.high}, Low: ${value.low}`);
// If using with long.js library
if (Long.isLong(value)) {
// It's an actual Long instance
console.log(`As string: ${value.toString()}`);
}
}
}
// Examples
const longValue = Long.fromString("9223372036854775807");
const longLike = { high: 2147483647, low: -1 };
processLongValue(longValue); // Both high/low properties and Long methods
processLongValue(longLike); // Just high/low propertiesinterface LongObject {
high: number;
low: number;
}