The Art of Type Arguments 9 exercises

Generics at Different Levels

In this exercise, we have a function called getHomePageFeatureFlags that takes in a config object that is currently typed as unknown:

export const getHomePageFeatureFlags = (
config: unknown,
override: (flags: unknown) => unknown
) => {
return override(config.rawConfig.fea

Loading exercise


0:00 In this exercise, we have a function called gethome pageFeatureFlags. What this does is it takes in a config object, which is currently typed as unknown, and what we have here is a function called override to override some of its flags.

0:14 This config object, it doesn't actually matter what we pass in, except that it has to have a config.rawConfig.featureFlags.home page. In other words, it has to have those deeply nested attributes. It can have other stuff, but this is really all we care about, this single deeply nested attribute in this config.

0:32 We have an example config here which matches up, so we have EXAMPLE_CONFIG rawConfig.featureFlags.home page. That's what we're doing our tests with, but you have to find a way to make this function generic.

0:46 What you can do from override is you can actually return different stuff. You should be able to call gethome pageFeatureFlags with this example config where we have the defaultFlags, which are going to be showBanner true, showLogOut.

0:58 We should be able to return a different version of that and show something different in there, showBanner instead. That's your job, is to try to find a way to express this with generics.

1:11 I'll give you a clue here. There is more than one right answer here. There is a little bit of art to this. The topic of this exercise is all about generics at different levels, so I want you thinking about that when you choose your approach.