Using the unknown keyword
Did you notice the unknown keyword used in the Fetch API example? Here's the code we are referring to: .then((jsonContent: unknown) >= ....
The unknown keyword was introduced with TypeScript 3.0. It is a type safe alternative to the any keyword that we have used so far in this book. With this keyword, we simply tell TypeScript that the type of the jsonContent object is not known at this point and that it should be checked before being manipulated. Basically, it means that the content should not be trusted and considered safe to use as is.
Anything can be assigned to a variable with the unknown type, but not the other way around. You cannot assign an object of type unknown to anything but itself and any unless you ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access