Narrow the unknown Type with TypeScript's Assertion Functions

Share this video with your friends

Send Tweet

This lesson introduces assertion functions which let us narrow the type of a variable or property for the remainder of the containing scope. We're going to learn how to create an assertion function by using the asserts keyword in the function signature. In particular, we're going to focus on narrowing the unknown type to a more specific type.

Additional Reading

Skylar Bolton
Skylar Bolton
~ 3 years ago

Well done! I can see multiple uses for this kind of reasoning and "defense" when writing open-source packages.

JooYoung Lee
JooYoung Lee
~ a year ago

It's the one of the most useful ones for me. Thx a lot!