Extract a Type from a Passer 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 something like this:
const logStarWarsPeopleResults = (data: typeof StarWarsPeopleResults)
However this won't work because that would represent the type of the Zod object itself rather than StarWarsPeopleResults
.
Challenge
Update the logStarWarsPeopleResults
function to extract a type from the passer object.
Transcript
Matt Pocock: We've got a bit of a problem on the type level, the type script level.
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.
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.
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.