Which fix removes the bug?
interface User { id: number; name: string }
const u = {} as User;
console.log(u.name.length);
- Build a real object literal
- Change
as Usertoas any - Mark both fields optional
- Declare
uwithlet
The assertion lies to the compiler:
Play Problem Solving ▸
u.name is undefined at runtime.