There are times when working with types where it's necessary to override the type of a variable, such as when incorrect types are provided by a library.
Casting is the process of overriding a type.
A straightforward way to cast a variable is using the
which will directly change the type of the given variable.
console.log((x as string).length);
Casting doesn't actually change the type of the data within the variable,
for example the following code with not work as expected since the variable
x is still holds a number.
console.log((x as string).length); // prints undefined since numbers don't have a length
TypeScript will still attempt to typecheck casts to prevent casts that don't seem correct,
for example the following will throw a type error since TypeScript knows casting a string to a number doesn't makes sense
without converting the data:
Using <> works the same as casting with
This type of casting will not work with TSX, such as when working on React files.
To override type errors that TypeScript may throw when casting, first cast to
unknown, then to the target type.
console.log(((x as unknown) as number).length); // x is not actually a number so this will return undefined