import { StackNode } from '.';

export class Stack<T> {
    public top: StackNode<T>;
    public size: number = 0;

    public push(data: T): void {
        this.top = new StackNode(data, this.top);
        ++this.size;
    }

    public pop(): T {
        if (this.isEmpty()) {
            throw new Error('Empty stack');
        }

        const data = this.top.data;
        this.top = this.top.next;

        --this.size;

        return data;
    }

    public peek(): T {
        if (this.isEmpty()) {
            throw new Error('Empty stack');
        }

        return this.top.data;
    }

    public isEmpty(): boolean {
        return this.size === 0;
    }
}

import { StackNode } from '.';

export class Stack<T> {
    public top: StackNode<T>;
    public size: number = 0;

    public push(data: T): void {
        this.top = new StackNode(data, this.top);
        ++this.size;
    }

    public pop(): T {
        if (!this.top) {
            throw new Error('Empty stack');
        }

        const data = this.top.data;
        this.top = this.top.next;

        --this.size;

        return data;
    }

    public peek(): T {
        if (!this.top) {
            throw new Error('Empty stack');
        }

        return this.top.data;
    }

    public isEmpty(): boolean {
        return this.size === 0;
    }
}

--- 
+++ 
@@ -10,7 +10,7 @@
     }
 
     public pop(): T {
-        if (!this.top) {
+        if (this.isEmpty()) {
             throw new Error('Empty stack');
         }
 
@@ -23,7 +23,7 @@
     }
 
     public peek(): T {
-        if (!this.top) {
+        if (this.isEmpty()) {
             throw new Error('Empty stack');
         }
 
Use isEmpty method for error checking
