Implement and Type Enum-Style Objects in TypeScript

Share this video with your friends

Send Tweet

In this lesson, we're going to learn how we can use const assertions to implement an enum-style object in TypeScript. We generally don't want widening of any literal types in this object, so we can use a const assertion. We're also going to compare our enum-style object to a native TypeScript enum.

felikf
felikf
~ 3 years ago

Thanks for explaining these two:

type ValuesOf<T> = T[keyof T]; // indexed access type - access the types of all properties in this object -
type MethodType = ValuesOf<typeof Method>; // Note that HTTPRequestMethod is a value, not a type, so we have to use the typeof operator here.

It is little bit difficult at first sight:)