TypeScript · Middle · Question 46 of 75
What does keyof typeof obj do?
const routes = { home: '/', about: '/about' };
type RouteName = keyof typeof routes;- ✓Correct answer: Gets the union of the
routesvalue's keys:'home' | 'about' - 2Gets the union of the values:
'/' | '/about' - 3Causes an error:
keyofcan't be applied totypeof
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.