$ ./guess_the_languageProblem Solving — fix the bug
Problem Solving — fix the bug · medium · #189

Which fix removes the bug?

interface User { name: string }
const u = {} as User;
console.log(u.name.length);
  1. Build a real User object
  2. Mark name as optional
  3. Add a non-null assert
  4. Widen User to any
The assertion only silences the compiler; at runtime name is undefined and .length throws.
Play Problem Solving ▸