Deque Implementation in TypeScript
14-day free trial
Try SeekerPro free for 14 days.
Full access while you try it · then $15.99/mo · cancel anytime
Start your 14-day free SeekerPro trial →30-day money-back guarantee · Cancel anytime
Not checking a site today? Turn this topic into a ready to paste AI prompt, free →
Key Takeaways
- ✓Deque runs in O(1) time and O(n) space, in TypeScript as in every other language.
- ✓Same runtime as JavaScript: no deque anywhere in lib.es*.d.ts, so you write one or take denque from npm (its @types are bundled).
- ✓Reviewers want the generic parameter on the class, not any, and want the internal buffer typed (T | undefined)[] with the undefined admitted honestly rather than asserted away with a non-null !.
- ✓The TypeScript trap: Types erase completely at compile time, so nothing in the type system stops the deque from being mutated while an iterator is mid-flight, and nothing enforces the element type at a boundary: a value arriving from JSON.parse or an untyped callsite lands in the buffer as the wrong shape and only fails much later at a use site.
- ✓Identical to JavaScript, because the emitted code is JavaScript. Two TypeScript-specific costs: downlevel iteration, if the target is below ES2015 and downlevelIteration is on, turns a for-of over the deque into a generator-driven loop with allocation per step; and enum or class field initialisers can add a constructor body you did not intend.
What Deque actually is
Deque in TypeScript: what you actually reach for
TypeScript implementation
export class Deque<T> implements Iterable<T> {
// undefined is admitted honestly rather than asserted away with !
#buf: (T | undefined)[];
#head = 0;
#size = 0;
constructor(capacity = 8) {
this.#buf = new Array<T | undefined>(capacity).fill(undefined);
}
get size(): number { return this.#size; }
push(value: T): void {
if (this.#size === this.#buf.length) this.#grow();
this.#buf[(this.#head + this.#size) % this.#buf.length] = value;
this.#size++;
}
// T | undefined, not T: the empty case is real at runtime and
// no type annotation can remove it.
shift(): T | undefined {
if (this.#size === 0) return undefined;
const v = this.#buf[this.#head];
this.#buf[this.#head] = undefined;
this.#head = (this.#head + 1) % this.#buf.length;
this.#size--;
return v as T;
}
#grow(): void {
const out = new Array<T | undefined>(this.#buf.length * 2).fill(undefined);
for (let i = 0; i < this.#size; i++) {
out[i] = this.#buf[(this.#head + i) % this.#buf.length];
}
this.#buf = out;
this.#head = 0;
}
*[Symbol.iterator](): Iterator<T> {
for (let i = 0; i < this.#size; i++) {
yield this.#buf[(this.#head + i) % this.#buf.length] as T;
}
}
}
Think Your Code Is Clean? Let NexusBro QA It in 20 Seconds.
Paste your code. Click QA. Get an instant expert-level audit with fixes.
QA My Code FreeThe TypeScript-specific trap
Performance in TypeScript, beyond the O(1)
Real-World Applications
Interview Tips & Practice
Frequently Asked Questions
Which TypeScript type or library should I use for Deque?
Same runtime as JavaScript: no deque anywhere in lib.es*.d.ts, so you write one or take denque from npm (its @types are bundled). What TypeScript adds is the type layer: declare the class as Deque<T> over a (T | undefined)[] backing store, and expose shift(): T | undefined so callers are forced by strictNullChecks to handle the empty case. If you back it with a TypedArray, the element type is fixed to number by Float64Array and no generic parameter can widen that.
What is the most common TypeScript mistake when implementing Deque?
Types erase completely at compile time, so nothing in the type system stops the deque from being mutated while an iterator is mid-flight, and nothing enforces the element type at a boundary: a value arriving from JSON.parse or an untyped callsite lands in the buffer as the wrong shape and only fails much later at a use site. Declaring shift(): T rather than T | undefined does not make the empty case go away; it just moves the undefined into code that believes it is impossible. Likewise a readonly Deque<T> is a compile-time annotation only: the underlying object is still mutable at runtime.
How does Deque actually perform in TypeScript?
Identical to JavaScript, because the emitted code is JavaScript. Two TypeScript-specific costs: downlevel iteration, if the target is below ES2015 and downlevelIteration is on, turns a for-of over the deque into a generator-driven loop with allocation per step; and enum or class field initialisers can add a constructor body you did not intend. Generic parameters cost nothing at runtime since they are erased. Prefer const enum-free, monomorphic call sites so the engine can inline the accessors. Time complexity is O(1) and space is O(n).
Related Articles
Rather try before you buy?
Get the rest of the vault: every cheat sheet, guide and audit in SeekerPro, free for 14 days, then $15.99/mo. Cancel anytime. Daily usage limits apply.
Start my 14-day SeekerPro trialMost AI and dev tools have a student plan. See which ones and how to claim them on Noizz.
Is your site built to last?
Run a free QA audit and get your Site Health Score in seconds.
Check Your Site FreeNo signup required
Reader comments
No account needed. Share what worked, what did not, and what you would add.
Add your take below. A concrete tip, a result or a question helps the next reader most.