The ability to reply to discussions is limited to PRO members. Want to join in the discussion? Click here to subscribe now.

Using Assertion to Convert Types in TypeScript

Using Assertion to Convert Types in TypeScript

2:09
Sometimes the compiler needs help figuring out a type. In this lesson we learn how to help out the compiler with Typescript type assertion.
Watch this lesson now
Avatar
egghead.io

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

Avatar
Sports

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)?

Avatar
Ari

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;
In reply to Sports
HEY, QUICK QUESTION!
Joel's Head
Why are we asking?