Conditional Types and Infer 10 exercises
Problem

Extract the Result From Several Possible Function Shapes

Let's imagine you're building a type helper to extract out the value from several different 'parsers'.

Here are a few different examples of what a parser can be.

The first is an object with a key of parse that is a function that returns a number:

const parser1 = {
  parse: () => 1,
}
Loading exercise

Transcript

0:00 In this exercise, we have several different understandings of what a parser can be. Here, it's an object with a key called parse which has a function which returns a number. Here, it's just a function that returns a string. Here, it's an object with extract which has a function that returns a Boolean.

0:20 Now, what your job is is to get the parser result where T can be any one of these things. We could parse in typeof parser1 and get back a number. We get typeof parser2 and get back a string. Typeof parser3, get back a Boolean.

0:37 Each time we're trying to extract the element from here. That's your job, is to try and work out how to express getparser results so that you can parse in any one of these and you'll still be able to infer the thing that's being grabbed.