Which fix removes the bug?
function isText(x: unknown): boolean {
return typeof x === 'string';
}
if (isText(v)) v.toUpperCase();
- Declare
x: anyas the type - Cast with
v as unknown - Add
!aftervin the call - Return type
x is string
A declared
Play Problem Solving ▸
boolean return does not narrow v, so v stays unknown; a type predicate tells the compiler what passed.