Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

low-router - hard type route names #46

Open
willybrauner opened this issue Sep 7, 2024 · 0 comments
Open

low-router - hard type route names #46

willybrauner opened this issue Sep 7, 2024 · 0 comments
Labels

Comments

@willybrauner
Copy link
Owner

willybrauner commented Sep 7, 2024

working example to contraint developer to pass existing route name to createUrl

import { Route } from "@wbe/low-router"

// routes 

const ROUTES = [
  {
    name: "home",
    children: [
      { name: "dashboard" },
      {
        name: "profile",
        children: [{ name: "hello" }, { name: "foo" }],
      },
    ],
  },
  { name: "about" },
  { name: "contact" },
] as const satisfies Route[] // this is the trick to allow route names casting 


// fake router

type RouteNames<R> = R extends { name: infer N; children: infer C }
  ? N | RouteNamesFromArray<C>
  : R extends { name: infer N }
    ? N
    : never

type RouteNamesFromArray<A> = A extends readonly (infer R)[] ? RouteNames<R> : never

class Router<T extends Route[]> {
  routes: T
  constructor(routes: T) {
    this.routes = routes
  }
  public createUrl({ name }: { name: RouteNamesFromArray<T> }) {
    console.log(`Creating URL for route name: ${name}`)
  }
}
const router = new Router(ROUTES)
router.createUrl({ name: "about" })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant