TypeScript Linked List
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.
Common errors
-
Argument of type 'string | number' is not assignable to parameter of type 'string'.
cause Attempting to initialize a `LinkedList<string>` with values that include numbers, or vice-versa, leading to a TypeScript type mismatch.fixEnsure all initial values match the generic type `T` specified for the `LinkedList`. For mixed types, explicitly use `new LinkedList<any>(...)`. -
TypeError: Cannot read properties of undefined (reading '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.fixBefore 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`). -
TypeError: linked_list_typescript_1.default is not a constructor
cause Attempting to import `LinkedList` as a default export (`import LinkedList from 'linked-list-typescript'`) when it is provided as a named export.fixCorrect the import statement to use named exports: `import { LinkedList } from 'linked-list-typescript';`
Warnings
- gotcha Incorrect type arguments for `LinkedList<T>` constructor will result in TypeScript compilation errors due to type inference or direct type checking.
- gotcha 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.
- gotcha 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.
Install
-
npm install linked-list-typescript -
yarn add linked-list-typescript -
pnpm add linked-list-typescript
Imports
- LinkedList
import LinkedList from 'linked-list-typescript';
import { LinkedList } from 'linked-list-typescript'; - LinkedList (CommonJS)
const LinkedList = require('linked-list-typescript');const { LinkedList } = require('linked-list-typescript'); - LinkedList<T> (Generics)
const list = new LinkedList();
const list = new LinkedList<string>();
Quickstart
import { LinkedList } from 'linked-list-typescript';
// Create a new linked list initialized with number values
let numberList = new LinkedList<number>(10, 20, 30);
console.log(`List length: ${numberList.length}`);
console.log(`Head value: ${numberList.head}`);
console.log(`Tail value: ${numberList.tail}`);
console.log("Iterating through the list using for...of:");
for (const item of numberList) {
console.log(item);
}
// Deconstruct elements
let [first, second] = numberList;
console.log(`Destructured: first=${first}, second=${second}`);
// Remove an element from the head
const removed = numberList.removeHead();
console.log(`Removed head: ${removed}`);
console.log(`New head: ${numberList.head}`);
// Example with a custom type
class Item {
constructor(public id: number, public name: string) {}
}
let item1 = new Item(1, "Apple");
let item2 = new Item(2, "Banana");
let itemList = new LinkedList<Item>(item1, item2);
console.log(`Custom type list head: ${itemList.head?.name}`); // Accessing property safely