$ ./guess_the_languageProblem Solving — fix the bug
Problem Solving — fix the bug · hard · #220

Which fix removes the bug?

function isText(x: unknown): boolean {
  return typeof x === 'string';
}
if (isText(v)) v.toUpperCase();
  1. Declare x: any as the type
  2. Cast with v as unknown
  3. Add ! after v in the call
  4. Return type x is string
A declared boolean return does not narrow v, so v stays unknown; a type predicate tells the compiler what passed.
Play Problem Solving ▸