TypeScript · Middle · Question 46 of 75

What does keyof typeof obj do?

const routes = { home: '/', about: '/about' };
type RouteName = keyof typeof routes;

Explanation

typeof routes gives the object's type, and keyof its keys. For a union of values you need indexed access: typeof routes[keyof typeof routes], and as const to make the values literals.

Further reading

Problem with a question

TypeScript · Middle · question 46

Test yourself

Take the TypeScript · Middle quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz