The problem with as
TL;DR: as
is not actually casting. Rather, it is type assertion, checking when compiling.
as
in other programming languages is doing something else than in TypeScript. In TypeScript, as
fades when transpiled to JS, thus, there are no checks at runtime. The keyword rather is a way of surpressing errors in the TS compiler.
A common issue is that when casting to an object with more properties than currently given, the compiler will later assume the object has those properties.
The alternative
Use Type guards instead.
Resources
A great article on this issue: https://medium.com/ingeniouslysimple/typescript-casts-are-not-casts-4c6c954749c4