Using Assertion to Convert Types in TypeScript

InstructorAri Picker

Share this video with your friends

Send Tweet

Sometimes the compiler needs help figuring out a type. In this lesson we learn how to help out the compiler with Typescript type assertion.

Sports Whispers
~ 7 years ago

So ... if type assertion no longer exists after the compilation, what's the point of it? 🙂 I guess the main purpose is to avoid the compiler error (in case we want to use different types where they don't share the same variable)?

Ari Pickerinstructor
~ 7 years ago

That's the basic idea. A good example would be if you want to create a variable that needs to be modified later, maybe with an ajax call...

interface Stuff { things: string; }

let stuff: Stuff = ({} as Stuff);

const ajax = "things";

stuff.things = ajax;