Vue router is elegant and type safe enough for most use cases. But I want to experiment how far typescript can help us.
Typesafe routing isn’t easy to do.
The easiest way to declare multiple class and instantatiate their paramater type. For example, typed-url. But this is too verbose.
Another approach is to use functional combinator. To put it simple, combinator is high order function that can abstract various operation. Routing combinators usually are a bunch of functions that can accept strings as static url segment or a function as dynamic url parameter. Both purescript and swift. But monad is too monad-y. My head just explodes.
One unique way to provide type routing is using reified generic! A demo video has illustrted how to implment it.(Spoiler: for a function with type
A => Response, one can access the class by
A.type and cast value by
guard let param: A = .... in swift. Whoa, reification is powerful). Github repo is here: https://github.com/NougatFramework/Switchboard
Compile time reflection is ideal for routing thing. Yesod uses template haskell to do this, Example. Macro paradise!
Scala has yet another unique construct called pattern match. Tumblr’s colossus is a great example to use pattern match for type safe routing.
And of course, haskell has many type safe routing library. Check out the review for more info.
Frankly type safety in router does not grant you much: it cannot check tempalte code, it can only help you to double check the shape of parameters in
$route. It can help you to type
router instance better, but requires all routes to have a
This is only a sketch for type safe routing design. Useful? No. Concise? Partly. Safety outweighs ease of use? No. Maybe it’s only suitable for type safe paranoid.