Registry / serialization / queue-typescript

queue-typescript

JSON →
library1.0.1jsnpmunverified

queue-typescript is a minimalist TypeScript library that provides a generic queue data structure. Currently at version 1.0.1, it offers a stable and lightweight solution for managing queues in TypeScript and JavaScript projects. The library distinguishes itself by its full support for TypeScript generics, allowing developers to create type-safe queues for any data type, including primitive types, objects, or custom classes, ensuring compile-time type checking. It also adheres to both the JavaScript iterator and iterable protocols, enabling seamless integration with modern JavaScript features such as `for...of` loops, the spread operator (`...`), and array deconstruction. Internally, `queue-typescript` relies on the `linked-list-typescript` package for its underlying data storage, contributing to efficient enqueue and dequeue operations. This package focuses on core queue functionality, delivering a straightforward, performant, and type-safe queue implementation without unnecessary overhead or additional utilities. Given its singular purpose and stable API, a rapid release cadence is not expected.

npm install queue-typescript
INSTALL
IMPORT
SIG · QUEUE-TYPESCRIPT
Q
queue-typescript
serializationjavascriptv1.0.1
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Queue
import { Queue } from 'queue-typescript';
This is the standard ESM import for TypeScript projects.
Queue
const { Queue } = require('queue-typescript');
import Queue from 'queue-typescript'; // Incorrect default import
While CommonJS `require` works, modern TypeScript projects typically use ESM imports. The `Queue` class is a named export, not a default export.
Queue (Type Usage)
let myQueue: Queue<string> = new Queue<string>();
let myQueue: Queue = new Queue(); // Lacks generic type parameter
Always specify the generic type parameter (e.g., `<string>`) when declaring or instantiating a `Queue` to leverage TypeScript's type safety features fully.

This example demonstrates how to create, initialize, enqueue items into, and iterate through a generic TypeScript Queue, showcasing its iterable protocol support.

import { Queue } from 'queue-typescript'; // Create a new queue initialized with numbers let numberQueue = new Queue<number>(10, 20, 30); console.log('Initial queue length:', numberQueue.length); // Expected: 3 console.log('Front element:', numberQueue.front); // Expected: 10 // Enqueue new items numberQueue.enqueue(40); numberQueue.enqueue(50); console.log('Queue after enqueuing:', numberQueue.length); // Expected: 5 // Iterate through the queue using for...of console.log('Queue elements:'); for (const item of numberQueue) { console.log(item); } // Expected: 10, 20, 30, 40, 50 (each on a new line) // Deconstruct the queue const [first, second, ...rest] = numberQueue; console.log('First element (deconstructed):', first); // Expected: 10 console.log('Second element (deconstructed):', second); // Expected: 20 console.log('Remaining elements (deconstructed):', rest); // Expected: [30, 40, 50] (or similar array representation)
Debug
Known issues
gotchaType Mismatch during Queue Initialization or Enqueuing: The `Queue` class uses generics for type safety. Attempting to initialize or enqueue values that do not match the specified generic type `T` will result in a TypeScript compilation error.
fix
Ensure all values passed to the `Queue` constructor or `enqueue` method conform to the generic type `T` declared for the queue instance. If varying types are expected, declare the queue with `Queue<any>`.
affects: >=1.0.0
gotchaEmpty Queue Operations: Attempting to access `queue.front` on an empty queue will return `undefined`.
fix
Always check `queue.length > 0` or specifically check if `queue.front` is `undefined` before attempting to use the returned value to avoid unexpected runtime errors.
affects: >=1.0.0
gotchaCommonJS (require) vs. ESM (import) Syntax: While the package README shows both `require` and `import` for `Queue`, modern TypeScript projects and Node.js environments increasingly favor ESM `import` statements. Using `require` might indicate an older project setup or specific configuration.
fix
For new TypeScript projects, prefer `import { Queue } from 'queue-typescript';`. If using CommonJS, ensure your `tsconfig.json` `module` option is set appropriately (e.g., `"CommonJS"`).
affects: >=1.0.0
Errors
Common errors & fixes
Argument of type 'number' is not assignable to parameter of type 'string'.
Attempting to create or add a value of an incorrect type to a generic queue that was initialized with a different type.
fix
Ensure the types of values being added match the generic type parameter of the `Queue`. Example: `let queue = new Queue<string>(); queue.enqueue(123);` will cause this error. Correct by using `queue.enqueue('hello');` or by initializing the queue as `new Queue<any>()`.
Property 'bar' does not exist on type 'undefined'.
Trying to access properties of an element (like `front`) when the queue is empty, resulting in `undefined`.
fix
Add a check for an empty queue before accessing elements: `if (queue.length > 0) { console.log(queue.front.bar); }`.
Upgrade
Version history
1.0.1latest on npm
Audit
Dependencies
linked-list-typescriptrequiredUsed as the underlying data structure for the queue implementation.
Agent activity
6 hits · last 30 days
node
6
Resources
queue-typescript — npm install queue-typescript · libregistry