TypeScript Only Features 9 exercises

String Enums in TypeScript

Here we have a request function that takes in a url and method parameter with strings for the type of request. There is also a Method object that has keys with string equivalents for GET, POST, PUT, and DELETE:

const Method = {

Loading exercise


00:00 In this exercise, we have a request function. This request function takes in a method. And this method has get, post, put, and delete as basically their string equivalents here. And the method down the bottom, or sorry, the request that has the parameter method

00:16 takes in get or post or put or delete. And we've got an interesting little conundrum here because what we want to do is we want to say this request function should force you to use the enum, to force you to use this object up here.

00:34 And it should require you to say method.get or method.post, for instance. And it should also give you an error if you pass a different enum with the same value. Don't ask me why we have these requirements. You know, we've got a crazy tech lead who's telling us really strange things. So your job is to try to find a different way

00:53 of kind of like expressing this const method and also a different way of expressing this type parameter here, or rather this function parameter annotation, because we are not going to get this behavior if we just use kind of the basic stuff in TypeScript, if we just use unions, if we just use objects.

01:12 So we're going to need an enum. Good luck.