Generic TypeScript Queue

1.0.1 · active · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

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)

view raw JSON →