- Learning TypeScript 2.x
- Remo H. Jansen
- 99字
- 2025-04-04 17:02:05
Type casting
The TypeScript type system allows us to cast a given type using two different syntaxes:
var myObject: TypeA;
var otherObject: any;
myObject = <TypeA> otherObject; // Using <>
myObject = otherObject as TypeA; // Using as keyword
It is important to understand that the TypeScript casting does not affect the runtime type of the variables.
Since Typescript 1.6, the default is as, because <> is ambiguous in .tsx files. We will learn more about .tsx files in Chapter 11 , Frontend Development with React and TypeScript.
In general, it is recommended to avoid using type castings and prefer generic types instead.