Extract a Type from a Parser Object
Now we've got a function to print our StarWarsPeopleResults to the console:
const logStarWarsPeopleResults = (data: unknown) => {
data.results.map((person) => {
console.log(person.name);
});
};
Once again, the data
type is unknown
.
As a fix, it may be tempting to try
Transcript
Matt Pocock: 0:00 We've got a bit of a problem on the type level, the type script level.
0:04 We've got this StarWarsPerson and we've got the StarWarsPeopleResults that we had from the last exercise, but now we've got this function that says logStarWarsPeopleResults. This data type here is unknown.
0:17 What we really want this to represent is we want this to represent StarWarsPeopleResults, right? We don't want it to represent this because if we do typeof StarWarsPeopleResults, that's not going to work because we actually want to represent the thing that it passes, not the actual object itself, or the Zod object itself.
0:38 That's a challenge for you. Zod actually does handle this really, really well, but I'll see if you can dive into the docs and work out how to extract a type from this passer object.