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-typescriptVerified import paths — ran on the pinned version, not inferred.
This example demonstrates how to create, initialize, enqueue items into, and iterate through a generic TypeScript Queue, showcasing its iterable protocol support.
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>`.
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.
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"`).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>()`.Add a check for an empty queue before accessing elements: `if (queue.length > 0) { console.log(queue.front.bar); }`.