Posts > TS Basic: Type Assertion
August 10, 2023
Sometimes you will have information about the type of value that TS can’t know about. Type assertions are used to explicitly specify the type of a value when the TS compiler is unable to infer it automatically.
<>
<>
to specify the target typelet someValue: any = 'Hello';
let strLength: number = (<string>someValue).length
as
keyword to specify the targetlet someValue: any = 'Hello';
let strLength: number = (someValue as string).length
👌They are simply a way to provide hints to the TS compiler for type checking during the compilation process