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
Specialized type checking for Node.js specific objects including buffers and streams with support for readable, writable, and duplex streams.
Returns true if value is a Buffer instance.
/**
* Returns true if val is buffer
* @param val - Value to check
* @returns Type guard indicating if value is Buffer
*/
function isBuffer(val: unknown): val is Buffer;Usage Example:
import { isBuffer } from "is-type-of";
const buf = Buffer.from("hello");
const arr = new Uint8Array([1, 2, 3]);
isBuffer(buf); // => true
isBuffer(arr); // => false
isBuffer("hello"); // => falseReturns true if value is a Stream instance.
/**
* Returns true if val is stream
* @param val - Value to check
* @returns Type guard indicating if value is Stream
*/
function isStream(val?: unknown): val is Stream;Usage Example:
import { isStream } from "is-type-of";
import { Readable } from "node:stream";
const readable = new Readable();
isStream(readable); // => true
isStream({}); // => falseReturns true if value is a readable stream.
/**
* Returns true if val is readable stream
* @param val - Value to check
* @returns Type guard indicating if value is Readable
*/
function isReadable(val?: unknown): val is Readable;Usage Example:
import { isReadable } from "is-type-of";
import { Readable, Writable } from "node:stream";
const readable = new Readable();
const writable = new Writable();
isReadable(readable); // => true
isReadable(writable); // => falseReturns true if value is a writable stream.
/**
* Returns true if val is write stream
* @param val - Value to check
* @returns Type guard indicating if value is Writable
*/
function isWritable(val?: unknown): val is Writable;Usage Example:
import { isWritable } from "is-type-of";
import { Readable, Writable } from "node:stream";
const readable = new Readable();
const writable = new Writable();
isWritable(writable); // => true
isWritable(readable); // => falseReturns true if value is a duplex stream (both readable and writable).
/**
* Returns true if val is duplex stream
* @param val - Value to check
* @returns Type guard indicating if value is Duplex
*/
function isDuplex(val?: unknown): val is Duplex;Usage Example:
import { isDuplex } from "is-type-of";
import { Duplex, Readable } from "node:stream";
const duplex = new Duplex();
const readable = new Readable();
isDuplex(duplex); // => true
isDuplex(readable); // => falseNode.js streams have a specific inheritance hierarchy:
The type checking functions respect this hierarchy:
import { isStream, isReadable, isDuplex } from "is-type-of";
import { Duplex } from "node:stream";
const duplex = new Duplex();
isStream(duplex); // => true (duplex is a stream)
isReadable(duplex); // => true (duplex is readable)
isDuplex(duplex); // => true (duplex is duplex)import { isReadable, isWritable, isDuplex } from "is-type-of";
function processStream(stream: unknown) {
if (isDuplex(stream)) {
console.log("Can both read from and write to this stream");
// stream is typed as Duplex
} else if (isReadable(stream)) {
console.log("Can read from this stream");
// stream is typed as Readable
} else if (isWritable(stream)) {
console.log("Can write to this stream");
// stream is typed as Writable
}
}import { isBuffer } from "is-type-of";
function processBufferLike(data: unknown) {
if (isBuffer(data)) {
// Node.js Buffer - has additional methods
console.log(data.toString('hex'));
} else if (data instanceof Uint8Array) {
// Browser-compatible typed array
console.log(Array.from(data));
}
}