{"id":11616,"library":"queue-typescript","title":"Generic TypeScript Queue","description":"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.","status":"active","version":"1.0.1","language":"javascript","source_language":"en","source_url":"https://github.com/sfkiwi/queue-typescript","tags":["javascript","typescript","linked-list","linkedlist","queue"],"install":[{"cmd":"npm install queue-typescript","lang":"bash","label":"npm"},{"cmd":"yarn add queue-typescript","lang":"bash","label":"yarn"},{"cmd":"pnpm add queue-typescript","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Used as the underlying data structure for the queue implementation.","package":"linked-list-typescript"}],"imports":[{"note":"This is the standard ESM import for TypeScript projects.","symbol":"Queue","correct":"import { Queue } from 'queue-typescript';"},{"note":"While CommonJS `require` works, modern TypeScript projects typically use ESM imports. The `Queue` class is a named export, not a default export.","wrong":"import Queue from 'queue-typescript'; // Incorrect default import","symbol":"Queue","correct":"const { Queue } = require('queue-typescript');"},{"note":"Always specify the generic type parameter (e.g., `<string>`) when declaring or instantiating a `Queue` to leverage TypeScript's type safety features fully.","wrong":"let myQueue: Queue = new Queue(); // Lacks generic type parameter","symbol":"Queue (Type Usage)","correct":"let myQueue: Queue<string> = new Queue<string>();"}],"quickstart":{"code":"import { Queue } from 'queue-typescript';\n\n// Create a new queue initialized with numbers\nlet numberQueue = new Queue<number>(10, 20, 30);\nconsole.log('Initial queue length:', numberQueue.length); // Expected: 3\nconsole.log('Front element:', numberQueue.front); // Expected: 10\n\n// Enqueue new items\nnumberQueue.enqueue(40);\nnumberQueue.enqueue(50);\nconsole.log('Queue after enqueuing:', numberQueue.length); // Expected: 5\n\n// Iterate through the queue using for...of\nconsole.log('Queue elements:');\nfor (const item of numberQueue) {\n  console.log(item);\n}\n// Expected: 10, 20, 30, 40, 50 (each on a new line)\n\n// Deconstruct the queue\nconst [first, second, ...rest] = numberQueue;\nconsole.log('First element (deconstructed):', first);   // Expected: 10\nconsole.log('Second element (deconstructed):', second); // Expected: 20\nconsole.log('Remaining elements (deconstructed):', rest); // Expected: [30, 40, 50] (or similar array representation)\n","lang":"typescript","description":"This example demonstrates how to create, initialize, enqueue items into, and iterate through a generic TypeScript Queue, showcasing its iterable protocol support."},"warnings":[{"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>`.","message":"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.","severity":"gotcha","affected_versions":">=1.0.0"},{"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.","message":"Empty Queue Operations: Attempting to access `queue.front` on an empty queue will return `undefined`.","severity":"gotcha","affected_versions":">=1.0.0"},{"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\"`).","message":"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.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"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>()`.","cause":"Attempting to create or add a value of an incorrect type to a generic queue that was initialized with a different type.","error":"Argument of type 'number' is not assignable to parameter of type 'string'."},{"fix":"Add a check for an empty queue before accessing elements: `if (queue.length > 0) { console.log(queue.front.bar); }`.","cause":"Trying to access properties of an element (like `front`) when the queue is empty, resulting in `undefined`.","error":"Property 'bar' does not exist on type 'undefined'."}],"ecosystem":"npm"}