stack-typescript is a lightweight, generic Stack data structure implementation for TypeScript and JavaScript environments, currently at version 1.0.4. It is built upon the `linked-list-typescript` package, providing a LIFO (Last-In, First-Out) collection. Key features include full TypeScript generics support for strong type-checking, enabling stacks of any primitive, object, or custom class. The package also implements both the JavaScript iterator and iterable protocols, allowing seamless integration with `for...of` loops, spread syntax (`...`), and array deconstruction. Its primary differentiators are its explicit use of a linked list for underlying storage and its full adherence to TypeScript's type-templating capabilities, ensuring type safety from initialization to manipulation. The release cadence appears stable, with a focus on core data structure functionality without frequent breaking changes, typical for foundational utility libraries.
npm install stack-typescriptVerified import paths — ran on the pinned version, not inferred.
Demonstrates creating, initializing, pushing, peeking, popping, and iterating a Stack with both primitive and custom types.
Be mindful of argument order during instantiation. If you want `a` to be at the bottom and `c` at the top, use `new Stack<string>('a', 'b', 'c');`.If deep copies are required to maintain immutability or isolated state, ensure you create copies of objects before pushing them onto the stack or before modifying them after retrieval.
Always explicitly define the generic type, e.g., `new Stack<MyType>()` or `new Stack<string>()`, to ensure type safety.
Ensure all values provided to the stack (during instantiation or via `push`) strictly adhere to the generic type parameter. For mixed types, use `Stack<any>` or a union type if appropriate (e.g., `Stack<string | number>`). ```typescript // Wrong: let items: (string | number)[] = ['one', 'two', 3]; let stack = new Stack<string>(...items); // Correct: let items: (string | number)[] = ['one', 'two', 3]; let stack = new Stack<string | number>(...items); ```