Registry / serialization / stack-typescript

stack-typescript

JSON →
library1.0.4jsnpmunverified

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-typescript
INSTALL
IMPORT
SIG · STACK-TYPESCRIPT
S
stack-typescript
serializationjavascriptv1.0.4
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Stack
import { Stack } from 'stack-typescript';
import Stack from 'stack-typescript';
Stack is a named export, not a default export.
Stack
const { Stack } = require('stack-typescript');
For CommonJS environments, destructure the named export directly.
Stack<T>
let myStack = new Stack<string>();
let myStack = new Stack();
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`.

Demonstrates creating, initializing, pushing, peeking, popping, and iterating a Stack with both primitive and custom types.

import { Stack } from 'stack-typescript'; // Create an empty stack of numbers let numberStack = new Stack<number>(); numberStack.push(10); numberStack.push(20); numberStack.push(30); console.log(`Number Stack Size: ${numberStack.size}`); // Expected: 3 console.log(`Top of Number Stack: ${numberStack.top}`); // Expected: 30 // Initialize a stack with values and custom types class Foo { constructor(public val: number) {} get bar(): number { return this.val; } } let foo1 = new Foo(100); let foo2 = new Foo(200); let fooStack = new Stack<Foo>(foo1, foo2, new Foo(300)); console.log(`Foo Stack Size: ${fooStack.size}`); // Expected: 3 console.log(`Top of Foo Stack: ${fooStack.top?.bar}`); // Expected: 100 let poppedFoo = fooStack.pop(); console.log(`Popped Foo: ${poppedFoo?.bar}`); // Expected: 100 console.log(`Foo Stack Size after pop: ${fooStack.size}`); // Expected: 2 // Iterate over the stack console.log('Iterating over Foo Stack:'); for (let item of fooStack) { console.log(item.bar); } // Expected: 200, 300
Debug
Known issues
gotchaWhen 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'.
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');`.
affects: >=1.0.0
gotchaThe 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.
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.
affects: >=1.0.0
gotchaUsing `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.
fix
Always explicitly define the generic type, e.g., `new Stack<MyType>()` or `new Stack<string>()`, to ensure type safety.
affects: >=1.0.0
Errors
Common errors & fixes
Argument of type 'string | number' is not assignable to parameter of type 'string'. Type 'number' is not assignable to type 'string'.
Attempting to initialize or push a value of a different type than the generic type parameter specified for the stack.
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>`).
```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); 
```
Upgrade
Version history
1.0.4latest on npm
Audit
Dependencies
linked-list-typescriptrequiredUsed as the underlying data structure for the Stack implementation.
Agent activity
4 hits · last 30 days
node
4
Resources
stack-typescript — npm install stack-typescript · libregistry