Can be used as a good alternative to as
.
const user = data as User;
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);
}