{"id":12067,"library":"stack-typescript","title":"TypeScript Stack with Generics","description":"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.","status":"active","version":"1.0.4","language":"javascript","source_language":"en","source_url":"https://github.com/sfkiwi/stack-typescript","tags":["javascript","typescript","linked-list","linkedlist","stack"],"install":[{"cmd":"npm install stack-typescript","lang":"bash","label":"npm"},{"cmd":"yarn add stack-typescript","lang":"bash","label":"yarn"},{"cmd":"pnpm add stack-typescript","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Used as the underlying data structure for the Stack implementation.","package":"linked-list-typescript","optional":false}],"imports":[{"note":"Stack is a named export, not a default export.","wrong":"import Stack from 'stack-typescript';","symbol":"Stack","correct":"import { Stack } from 'stack-typescript';"},{"note":"For CommonJS environments, destructure the named export directly.","symbol":"Stack","correct":"const { Stack } = require('stack-typescript');"},{"note":"Always specify the generic type parameter `<T>` when initializing a new Stack to leverage TypeScript's full type-checking capabilities. Omitting it will default to `any`.","wrong":"let myStack = new Stack();","symbol":"Stack<T>","correct":"let myStack = new Stack<string>();"}],"quickstart":{"code":"import { Stack } from 'stack-typescript';\n\n// Create an empty stack of numbers\nlet numberStack = new Stack<number>();\nnumberStack.push(10);\nnumberStack.push(20);\nnumberStack.push(30);\n\nconsole.log(`Number Stack Size: ${numberStack.size}`); // Expected: 3\nconsole.log(`Top of Number Stack: ${numberStack.top}`); // Expected: 30\n\n// Initialize a stack with values and custom types\nclass Foo {\n  constructor(public val: number) {}\n  get bar(): number { return this.val; }\n}\n\nlet foo1 = new Foo(100);\nlet foo2 = new Foo(200);\nlet fooStack = new Stack<Foo>(foo1, foo2, new Foo(300));\n\nconsole.log(`Foo Stack Size: ${fooStack.size}`); // Expected: 3\nconsole.log(`Top of Foo Stack: ${fooStack.top?.bar}`); // Expected: 100\n\nlet poppedFoo = fooStack.pop();\nconsole.log(`Popped Foo: ${poppedFoo?.bar}`); // Expected: 100\nconsole.log(`Foo Stack Size after pop: ${fooStack.size}`); // Expected: 2\n\n// Iterate over the stack\nconsole.log('Iterating over Foo Stack:');\nfor (let item of fooStack) {\n  console.log(item.bar);\n}\n// Expected: 200, 300\n","lang":"typescript","description":"Demonstrates creating, initializing, pushing, peeking, popping, and iterating a Stack with both primitive and custom types."},"warnings":[{"fix":"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');`.","message":"When initializing a Stack with values (e.g., `new Stack<number>(...items)`), the items are pushed from left-to-right into the stack. This means the *first* argument provided will become the *top* of the stack, and the *last* argument will be at the *bottom*. This is inverse to how an array `push` would naturally order elements if interpreted as 'first in, first at bottom'.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"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.","message":"The Stack stores references to objects, not copies. Modifying an object retrieved from the stack (e.g., via `top` or `pop()`) will affect the object within the stack if it is later pushed back, or if other references to the same object exist.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Always explicitly define the generic type, e.g., `new Stack<MyType>()` or `new Stack<string>()`, to ensure type safety.","message":"Using `new Stack()` without specifying a generic type parameter `<T>` will result in a stack of type `Stack<any>`. This will bypass TypeScript's type-checking and defeat the purpose of using a generic data structure.","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 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>`).\n```typescript\n// Wrong:\nlet items: (string | number)[] = ['one', 'two', 3];\nlet stack = new Stack<string>(...items); \n\n// Correct:\nlet items: (string | number)[] = ['one', 'two', 3];\nlet stack = new Stack<string | number>(...items); \n```","cause":"Attempting to initialize or push a value of a different type than the generic type parameter specified for the stack.","error":"Argument of type 'string | number' is not assignable to parameter of type 'string'. Type 'number' is not assignable to type 'string'."}],"ecosystem":"npm"}