Skip to content

Instantly share code, notes, and snippets.

@ghostdevv
Last active August 17, 2021 19:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ghostdevv/4ab87a6caaee1d23acbcd1d12336cc36 to your computer and use it in GitHub Desktop.
Save ghostdevv/4ab87a6caaee1d23acbcd1d12336cc36 to your computer and use it in GitHub Desktop.
Type safe state in a file based router

Type safe state in a file based router

Here is the golden question, imagine we have a file based routing solution and we want state. Say to pass down the database connection, or some auth guards to avoid the ../../... hell. Whatever it is it needs to be easy to use and type safe

Say we have this:

import Router from 'packge';

const router = new Router('src/routes');

We can do whatever to Router class to support props, but how do we have them in the files?

src/routes/test.js

export const test = () => {
  const db = ... ?
}

How do we pass db down to test in a type safe and simple way?

It's not just about database, it's about any props. The example is just db as that will be the most common prop passed down

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment