Generic TypeScript Queue
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.
Common errors
-
Argument of type 'number' is not assignable to parameter of type 'string'.
cause Attempting to create or add a value of an incorrect type to a generic queue that was initialized with a different type.fixEnsure 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'.
cause Trying to access properties of an element (like `front`) when the queue is empty, resulting in `undefined`.fixAdd a check for an empty queue before accessing elements: `if (queue.length > 0) { console.log(queue.front.bar); }`.
Warnings
- gotcha Type 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.
- gotcha Empty Queue Operations: Attempting to access `queue.front` on an empty queue will return `undefined`.
- gotcha CommonJS (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.
Install
-
npm install queue-typescript -
yarn add queue-typescript -
pnpm add queue-typescript
Imports
- Queue
import { Queue } from 'queue-typescript'; - Queue
import Queue from 'queue-typescript'; // Incorrect default import
const { Queue } = require('queue-typescript'); - Queue (Type Usage)
let myQueue: Queue = new Queue(); // Lacks generic type parameter
let myQueue: Queue<string> = new Queue<string>();
Quickstart
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)