typestar

Generic stack in TypeScript

A generic stack, and a first look at what a type parameter buys you.

class Stack<T> {
  private items: T[] = [];

  push(item: T): void {
    this.items.push(item);
  }

  pop(): T | undefined {
    return this.items.pop();
  }

  get size(): number {
    return this.items.length;
  }
}

Keywords and builtins used here

The run, in numbers

Lines
15
Characters to type
192
Tokens
69
Three-star pace
110 tpm

At the three-star pace of 110 tokens a minute, this run takes about 38 seconds.

Type this snippet

Step 4 of 5 in Building & injecting, step 12 of 19 in Classes & patterns.

← Previous Next →