{"id":11239,"library":"linked-list-typescript","title":"TypeScript Linked List","description":"The `linked-list-typescript` package provides a straightforward, type-safe implementation of a doubly linked list in TypeScript. Currently stable at version 1.0.15, it emphasizes simplicity and strong typing through TypeScript generics. The library supports the JavaScript `Iterator` and `Iterable` protocols, enabling native iteration via `for...of` loops, spread syntax (`...`), and array destructuring. Key operations like retrieving head/tail elements, and removing head/tail are available. Unlike some data structures, it stores references to values rather than copies, which is important for understanding its behavior with mutable objects. The project appears to be in an active maintenance phase, with a focus on a stable 1.x release line.","status":"active","version":"1.0.15","language":"javascript","source_language":"en","source_url":"https://github.com/sfkiwi/linked-list-typescript","tags":["javascript","typescript","linked-list","linkedlist"],"install":[{"cmd":"npm install linked-list-typescript","lang":"bash","label":"npm"},{"cmd":"yarn add linked-list-typescript","lang":"bash","label":"yarn"},{"cmd":"pnpm add linked-list-typescript","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"LinkedList is a named export. Ensure to use destructuring in the import statement. Both ESM and CommonJS named imports are supported.","wrong":"import LinkedList from 'linked-list-typescript';","symbol":"LinkedList","correct":"import { LinkedList } from 'linked-list-typescript';"},{"note":"For CommonJS environments, LinkedList is exported as a named property of the module object.","wrong":"const LinkedList = require('linked-list-typescript');","symbol":"LinkedList (CommonJS)","correct":"const { LinkedList } = require('linked-list-typescript');"},{"note":"While `new LinkedList()` might work in some contexts, explicitly defining the generic type `T` (e.g., `<string>`) provides strong type checking and is recommended for TypeScript projects. Use `LinkedList<any>` for mixed types.","wrong":"const list = new LinkedList();","symbol":"LinkedList<T> (Generics)","correct":"const list = new LinkedList<string>();"}],"quickstart":{"code":"import { LinkedList } from 'linked-list-typescript';\n\n// Create a new linked list initialized with number values\nlet numberList = new LinkedList<number>(10, 20, 30);\n\nconsole.log(`List length: ${numberList.length}`);\nconsole.log(`Head value: ${numberList.head}`);\nconsole.log(`Tail value: ${numberList.tail}`);\n\nconsole.log(\"Iterating through the list using for...of:\");\nfor (const item of numberList) {\n    console.log(item);\n}\n\n// Deconstruct elements\nlet [first, second] = numberList;\nconsole.log(`Destructured: first=${first}, second=${second}`);\n\n// Remove an element from the head\nconst removed = numberList.removeHead();\nconsole.log(`Removed head: ${removed}`);\nconsole.log(`New head: ${numberList.head}`);\n\n// Example with a custom type\nclass Item {\n    constructor(public id: number, public name: string) {}\n}\nlet item1 = new Item(1, \"Apple\");\nlet item2 = new Item(2, \"Banana\");\n\nlet itemList = new LinkedList<Item>(item1, item2);\nconsole.log(`Custom type list head: ${itemList.head?.name}`); // Accessing property safely\n","lang":"typescript","description":"Demonstrates how to create a generic linked list, initialize it with values, iterate over its elements, deconstruct, and perform basic operations like removing the head, including usage with custom types."},"warnings":[{"fix":"Ensure all initial values passed to the `LinkedList` constructor conform to the specified generic type `T`. Use `LinkedList<any>` if heterogeneous types are intentionally required.","message":"Incorrect type arguments for `LinkedList<T>` constructor will result in TypeScript compilation errors due to type inference or direct type checking.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Always check if the list is empty (e.g., `list.length === 0` or `list.head !== undefined`) before attempting to access `head` or `tail` and their properties.","message":"Accessing `head` or `tail` properties on an empty `LinkedList` instance will return `undefined` instead of throwing an error. This can lead to runtime issues if not checked.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Be aware that changes to referenced objects will be reflected in the list. If immutable values are desired, ensure objects are not modified after insertion or create defensive copies before adding them.","message":"The `LinkedList` stores values by reference, not by copy. Modifying objects after they have been added to the list will affect the values within the list.","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 all initial values match the generic type `T` specified for the `LinkedList`. For mixed types, explicitly use `new LinkedList<any>(...)`.","cause":"Attempting to initialize a `LinkedList<string>` with values that include numbers, or vice-versa, leading to a TypeScript type mismatch.","error":"Argument of type 'string | number' is not assignable to parameter of type 'string'."},{"fix":"Before accessing properties on `list.head` or `list.tail`, verify that `list.head` (or `list.tail`) is not `undefined`, typically by checking `list.length > 0` or using optional chaining (`list.head?.property_name`).","cause":"Attempting to access a property on `list.head` or `list.tail` when the list is empty, causing `head`/`tail` to return `undefined` and subsequent property access to fail.","error":"TypeError: Cannot read properties of undefined (reading 'property_name')"},{"fix":"Correct the import statement to use named exports: `import { LinkedList } from 'linked-list-typescript';`","cause":"Attempting to import `LinkedList` as a default export (`import LinkedList from 'linked-list-typescript'`) when it is provided as a named export.","error":"TypeError: linked_list_typescript_1.default is not a constructor"}],"ecosystem":"npm"}