Single Source of Truth

Single Source of Truth
πŸ‘¨β€πŸ’Ό We want a single source of truth for route names and paths without enums. Using as const lets us derive types directly from the runtime object.
🐨 Open
index.ts
and:
  1. Add as const to the routes object so routes.home stays the literal '/'
  2. Create RouteName from the keys of routes and RoutePath from the values
  3. Remove the @ts-expect-error once defaultRoute: '/' type-checks
  4. Implement getRoutePath(name: RouteName): RoutePath so it returns the path for that name from routes
  5. Export routes, defaultRoute, and getRoutePath by name

Completion criteria

  • Named exports: routes, defaultRoute, getRoutePath
  • getRoutePath('home') β†’ '/'
  • getRoutePath('login') β†’ '/login'
  • getRoutePath('settings') β†’ '/settings'
πŸ’° keyof typeof gives you the keys; indexed access gives you the values.

Please set the playground first

Loading "Single Source of Truth"
Loading "Single Source of Truth"