Deriving Types From Values 15 exercises
Problem

The typeof Operator

Here we have a similar setup to last time, but this time instead of the source of truth being a type, we want it to be a value.

Here, we have an object named configurations that comprises a set of deployment environments for development, production, and staging.

Each environment has its

Loading exercise

Transcript

00:00 Here we've got a setup pretty similar to the previous exercise, but this time instead of the source of truth being a type, we want it to be a value. Here we have some configurations, and these configurations are basically a set of environments. We have the development environment, the production environment, and the staging environment.

00:18 These are three environments that we can deploy to that have different API base URLs and different timeout settings it seems as well. Our development environment, our production environment, our staging environment, we want to take these and use them as a type that we can use further out in our application, in different setups.

00:36 We have declared this environment type here, development or production or staging. But my challenge to you is, can you, you can see there's a bit of duplication here again. We have two sources of truth where really we should only have one source of truth. We could, of course, say, okay, environment is the source of truth,

00:55 and then we say in configurations, sure, this is now a record of environments that has API base URL string timeout number like this. But I actually want to go the other way, and I want to say, okay, let's actually declare our type from our value,

01:14 which is going to just result in a bit less code, I think. That's your job. You can change this line of code down here, the environment line of code, and try to make it the key of this value. There is an extra little piece of TypeScript syntax that you're going to be

01:30 needing and it's called typeof. Good luck.