Casting
Casting Types
Section titled “Casting Types”Casting overrides the variable type, which you may want to do when you know more about the type than the compiler does.
Casting with as
Section titled “Casting with as”Tell the compiler to treat a value as a specific type:
Casting doesn’t change the actual value - it just tells TypeScript how to treat it:
TypeScript Typechecks Casts
Section titled “TypeScript Typechecks Casts”Casting using <>
Section titled “Casting using <>”Note: doesn’t work in TSX.
Forced casting
Section titled “Forced casting”You can force cast to any type by casting to unknown, then to the type you want. This will override any errors thrown by casting.