HardClass Patterns🐛 Debug Challenge

Class missing interface implementation — methods silently absent

Buggy Code — Can you spot the issue?

// TypeScript: Class 'SimpleLogger' incorrectly implements interface 'Logger'.
// Property 'warn' is missing in type 'SimpleLogger' but required in type 'Logger'.

// Interface contract
const Logger = {
  info: null,
  warn: null,   // Required but missing in implementation
  error: null,
};

class SimpleLogger {
  prefix;

  constructor(prefix) {
    this.prefix = prefix;
  }

  info(msg) {
    console.log('[INFO]' + this.prefix + ': ' + msg);
  }

  // Bug: warn() and error() not implemented — calling them returns undefined
  // TypeScript error: Class incorrectly implements interface
}

const logger = new SimpleLogger('App');
logger.info('started');

// Bug: warn is not a function — silently wrong
const result = typeof logger.warn;
console.log(result);  // 'undefined' — method missing

Fixed Code

class SimpleLogger {
  prefix;

  constructor(prefix) {
    this.prefix = prefix;
  }

  info(msg) {
    console.log('[INFO] ' + this.prefix + ': ' + msg);
  }

  // Fix: implement all required interface methods
  warn(msg) {
    console.log('[WARN] ' + this.prefix + ': ' + msg);
  }

  error(msg) {
    console.log('[ERROR] ' + this.prefix + ': ' + msg);
  }
}

const logger = new SimpleLogger('App');
logger.info('started');
logger.warn('slow response');

const result = typeof logger.warn;
console.log(result);

Bug Explained

Bug: SimpleLogger does not implement warn() and error() from the Logger interface. TypeScript would error: "Class incorrectly implements interface — warn is missing." Callers get undefined when calling missing methods.

Explanation: All interface methods are implemented. warn() is now a function, not undefined. TypeScript's implements keyword enforces this at compile time — any missing method is a compile error.

Key Insight: In TypeScript, implements InterfaceName is a compile-time assertion that your class satisfies the contract. It does NOT provide implementation — you must write each method. This gives you early error detection when you add methods to an interface later.

More Class Patterns Debug Challenges

MediumPrivate field accessed outside class — wrong data exposedMediumAbstract class instantiated directly — unexpected behavior

Practice spotting bugs live →

38 debug challenges with AI hints

🐛 Try Debug Lab