CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-is-type-of

Complete type checking utility library for Node.js with TypeScript support and type guards

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

node-objects.mddocs/

Node.js Object Type Checking

Specialized type checking for Node.js specific objects including buffers and streams with support for readable, writable, and duplex streams.

Capabilities

Buffer Type Checking

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");      // => false

Stream Type Checking

Returns 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({});           // => false

Readable Stream Type Checking

Returns 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);   // => false

Writable Stream Type Checking

Returns 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);   // => false

Duplex Stream Type Checking

Returns 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);     // => false

Stream Type Hierarchy

Node.js streams have a specific inheritance hierarchy:

  • Stream: Base class for all streams
  • Readable: Streams you can read from
  • Writable: Streams you can write to
  • Duplex: Streams that are both readable and writable

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)

Common Use Cases

Processing Different Stream Types

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
  }
}

Buffer vs Typed Array Detection

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));
  }
}

docs

external-objects.md

index.md

node-objects.md

primitive-types.md

standard-objects.md

tile.json