1 min read 90 words Updated Sep 24, 2026 Created Sep 24, 2026
#TypeScript#review

Can be used as a good alternative to as, because aswill not actually check at runtime. Type guards help us to prevent bugs in production.

Rather than:

const user = data as User;

Write:

function isUser(obj: any): obj is User {
  return typeof obj === 'object' &&
         obj !== null &&
         typeof obj.name === 'string' &&
         typeof obj.age === 'number';
}

if (isUser(data)) {
  // TypeScript now knows `data` is a User
  console.log(data.name);
}

Important foundations for type guards are:

  • in
  • typeof
  • instanceof