{"id":12758,"library":"typescript-collections","title":"TypeScript Collections","description":"typescript-collections is a robust and fully tested data structure library written in TypeScript, currently at version 1.3.3. It provides a comprehensive suite of common data structures, including Linked Lists, Dictionaries (regular, multi, and default), Binary Search Trees, Stacks, Queues, Sets, Bags, Binary Heaps, and Priority Queues, alongside array utility functions. The library leverages TypeScript Generics to offer strong type safety and enhanced developer experience through IntelliSense. It supports UMD (Universal Module Definition), making it suitable for use in Node.js, modern browsers, and other JavaScript environments. A key design aspect for its hashing-based collections (like `Dictionary` and `Set`) is the reliance on an item's `toString()` method for equality checks, necessitating custom implementations for non-primitive keys to ensure correct behavior. The package was last published 6 years ago, suggesting it is in a maintenance phase with infrequent updates.","status":"maintenance","version":"1.3.3","language":"javascript","source_language":"en","source_url":"https://github.com/basarat/typescript-collections","tags":["javascript","typescript","generics","data","structures","collections","linked","list","dictionary"],"install":[{"cmd":"npm install typescript-collections","lang":"bash","label":"npm"},{"cmd":"yarn add typescript-collections","lang":"bash","label":"yarn"},{"cmd":"pnpm add typescript-collections","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"This is the recommended ES Module import pattern for TypeScript and modern JavaScript environments. All data structures are exposed as properties of the 'Collections' object (e.g., `new Collections.Set()`).","symbol":"Collections","correct":"import * as Collections from 'typescript-collections';"},{"note":"This is the legacy TypeScript-specific CommonJS import syntax. While technically still functional, the `import * as` syntax is generally preferred in modern TypeScript projects that target ES Modules.","wrong":"import * as Collections from 'typescript-collections';","symbol":"Collections","correct":"import Collections = require('typescript-collections');"},{"note":"This is the standard CommonJS import pattern for Node.js environments. The library does not provide a default export, so `import Collections from '...'` will fail.","wrong":"import Collections from 'typescript-collections';","symbol":"Collections","correct":"var Collections = require('typescript-collections');"},{"note":"The `makeString` utility for creating string representations of objects is nested under `Collections.util`.","symbol":"util.makeString","correct":"import * as Collections from 'typescript-collections';\nconst str = Collections.util.makeString(myObject);"}],"quickstart":{"code":"import * as Collections from 'typescript-collections';\n\n// Demonstrate Set usage with generics for type safety\nvar mySet = new Collections.Set<number>();\nmySet.add(123);\nmySet.add(456);\nmySet.add(123); // Duplicate, will not be added to the set\n\nconsole.log(`Set size: ${mySet.size()}`); // Expected output: Set size: 2\nconsole.log(`Set contains 456: ${mySet.contains(456)}`); // Expected output: Set contains 456: true\n\n// Demonstrate Queue usage\nvar myQueue = new Collections.Queue<string>();\nmyQueue.enqueue(\"first\");\nmyQueue.enqueue(\"second\");\n\nconsole.log(`Dequeued: ${myQueue.dequeue()}`); // Expected output: Dequeued: first\nconsole.log(`Dequeued: ${myQueue.dequeue()}`); // Expected output: Dequeued: second\nconsole.log(`Queue is empty: ${myQueue.isEmpty()}`); // Expected output: Queue is empty: true\n\n// Example of custom object as a key in a Dictionary, requiring custom toString()\nclass User {\n    constructor(public id: number, public name: string) {}\n    toString() { // Crucial for correct hashing behavior in Collections.Dictionary/Set\n        return `User-${this.id}`;\n    }\n}\n\nconst userDict = new Collections.Dictionary<User, string>();\nconst user1 = new User(1, \"Alice\");\nconst user2 = new User(2, \"Bob\");\nuserDict.setValue(user1, \"Administrator\");\nuserDict.setValue(user2, \"Guest\");\n\nconsole.log(`User 1 role: ${userDict.getValue(user1)}`); // Expected output: User 1 role: Administrator","lang":"typescript","description":"Demonstrates basic usage of `Set` and `Queue` with generics for type safety, and illustrates how to use custom objects as keys in a `Dictionary` by implementing a `toString()` method for proper hashing."},"warnings":[{"fix":"Implement a custom `toString()` method on your key/value objects (if they are not primitives) that accurately represents their identity. Example: `class MyObject { /* ... */ toString() { return JSON.stringify(this); } }`.","message":"For hashing-based collections like `Dictionary` and `Set`, `typescript-collections` relies on the `toString()` method of objects for equality and uniqueness checks. If you use custom objects as keys or values in these collections, you *must* implement a `toString()` method that provides a unique and consistent string representation for equal objects. Otherwise, behavior may be incorrect (e.g., duplicates being added, or `contains` failing).","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Add or update your `tsconfig.json` to include: `\"compilerOptions\": { \"moduleResolution\": \"node\" }`. Ensure this aligns with your project's overall module resolution strategy.","message":"To ensure TypeScript properly resolves the type definitions, particularly in older or non-standard configurations, you may need to explicitly set `\"moduleResolution\": \"node\"` in your `tsconfig.json`'s `compilerOptions`.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Add a script tag to your HTML: `<script src=\"[path_to_node_modules]/typescript-collections/dist/lib/umd.min.js\"></script>`","message":"When using `typescript-collections` directly in a browser environment without a bundler, you must manually include the UMD bundle. This is typically `dist/lib/umd.min.js` (or `umd.js`).","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Ensure your project uses TypeScript 0.9 or newer. Modern projects should use a much more recent TypeScript version (e.g., TypeScript 4.x or 5.x).","message":"`typescript-collections` explicitly requires TypeScript 0.9 or above due to its reliance on TypeScript Generics. Older TypeScript versions are not supported.","severity":"breaking","affected_versions":"<1.0.0 (historical) but applies to projects with ancient TS versions"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Ensure the type of the element being added/inserted/queued matches the generic type argument of the collection instance. For example, if `mySet` is `Set<number>`, only add `number` values.","cause":"Attempting to add an element of a type that does not match the generic type argument specified for the collection (e.g., adding a `string` to a `Set<number>`).","error":"Argument of type 'string' is not assignable to parameter of type 'number'."},{"fix":"Verify `typescript-collections` is installed (`npm install typescript-collections`). Check `tsconfig.json` for `\"compilerOptions\": { \"moduleResolution\": \"node\" }`. Ensure the import statement is `import * as Collections from 'typescript-collections';`.","cause":"The TypeScript compiler is unable to locate the package or its type definitions. Common reasons include missing `\"moduleResolution\": \"node\"` in `tsconfig.json`, incorrect installation, or an invalid import path.","error":"Cannot find module 'typescript-collections' or its corresponding type declarations."},{"fix":"Implement a `toString()` method on your custom object classes that returns a unique string representation for distinct objects and an identical string for equal objects. Example: `class MyKey { id: string; toString() { return this.id; } }`.","cause":"`typescript-collections` uses the `toString()` method for object equality checks in hashing-based collections. If custom objects do not have a meaningful `toString()` implementation, they may be treated as unequal even if their contents are the same, or vice versa.","error":"Object equality for custom types not working as expected in collections like `Dictionary` or `Set`."}],"ecosystem":"npm"}