class Lol {
get length() {
console.log('Lol. LMAO even.');
return 5;
}
}
const noLolsAllowed = (notALol: string) => {
for (let i = 0; i < notALol.length; ++i) {
console.log('No lols here');
}
}
noLolsAllowed(new Lol() as unknown as string);


asis an escape hatch and super dangerous. I think they shouldn’t have given it such a nice name.unsafeCastAswould have been much better IMHO.It does require the uglier
as unknown asin cases where it can provide that the cast is impossible. But that still allows many cases such asFoo|undefined as Bar|undefinedworking fine (TypeScript assumesundefinedand doesn’t require the cast viaunknown).