typestar

Generic queue in TypeScript

A generic FIFO queue, the counterpart to the stack.

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

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

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

  get isEmpty(): boolean {
    return this.items.length === 0;
  }
}

Keywords and builtins used here

The run, in numbers

Lines
15
Characters to type
211
Tokens
71
Three-star pace
110 tpm

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

Type this snippet

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

← Previous Next →