Actually this cast is safe in TypeScript, additional variables are unnecessary.
The statement ‘const d: Dog’ and the expression ‘d as Dog’ are equivalent w.r.t. type safety. The compiler will error in both cases the if the value is not a Dog.
The statement ‘const d: Dog’ and the expression ‘d as Dog’ are equivalent w.r.t. type safety. The compiler will error in both cases the if the value is not a Dog.
interface Dog {
breed: string;
}
const dog = {} as Dog;
console.log(dog.breed);
Using `as` is very unsafe in TypeScript.I've collected more quirks of `as` in this playground: https://www.typescriptlang.org/play/index.html#code/PTAEAMEs...