Sobes.tech
Middle

What is an abstract class? What happens if a developer writes ts-ignore and does not implement the abstract class methods?

sobes.tech AI

Answer from AI

An abstract class in TypeScript is used to define a base class that cannot be instantiated directly but serves as a template for other classes. It can contain both implemented methods and abstract methods — declarations without implementation that must be implemented by subclasses.

If a developer applies // @ts-ignore and does not implement the abstract methods, TypeScript will not throw a compilation error, but at runtime, an error may occur if the unimplemented method is called. This violates the contract of the abstract class and can lead to potential bugs, as the expected logic in the methods is missing.

Example:

abstract class Animal {
  abstract makeSound(): void;
  move() {
    console.log('Moving...');
  }
}

class Dog extends Animal {
  // @ts-ignore
  // makeSound is not implemented
}

const dog = new Dog();
dog.makeSound(); // Runtime error: makeSound is not defined