Type Assertions
A type assertion tells the compiler to treat a value as a specific type using the as keyword. It changes nothing at runtime.
Assertions are useful when you know more about a value than the compiler does, such as a broad type you want to narrow.
const raw: unknown = "hello";
const text = raw as string;
Use assertions sparingly. A wrong assertion hides real type errors.
Try it yourself
Loading editor…