We have a superhero and a bad guy. Let's make a function that saves the day if the function's argument is a superhero, and a commits a bad deed if its argument is a bad guy. Our function needs to accept something that could be a superhero or a bad guy.
The IDE is telling us something's wrong. Let's see what compiler says. Something doesn't have powers. This is because the compiler is evaluating both types of the union-type argument. Since the bad guy doesn't have powers, something doesn't have powers. We can get a hold of the superhero's power's property by asserting that something is a superhero.
An assertion is how we told the compiler, "We have some information about something's type that it doesn't." There are two different syntaxes for assertion. We're using the as type syntax, which goes behind the value. We're putting something in parens in order to isolate it from its property. If we remove the parens we can't make the assertion.
The other syntax is with angle brackets, and it goes before the value. The angle bracket syntax was the original syntax, but it conflicted with JSX, so the as type syntax was created.
Let's finish our if block by calling the superhero saves the day method. We need to add the else block. Let's call our function with both types. Let's see if it works.
To review, we used TypeScript's type assertions when we need to tell the compiler that we know more about something's type than it does. They can be written with the as type syntax or the angle bracket syntax unless you're using JSX.