type assertion

Tells the compiler to treat a value as a specific type when you know more than the compiler does. Does not perform runtime conversion.

Syntax

typescript
value as Type
<Type>value  // older syntax

Example

typescript
const input = document.getElementById("name") as HTMLInputElement;
const value = input.value;

const data = JSON.parse(response) as UserData;

// Double assertion (use cautiously):
const x = someValue as unknown as SpecificType;