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

TypeScript compilerOptions -> "strict" : true #8

Open
cgers opened this issue Mar 11, 2019 · 2 comments
Open

TypeScript compilerOptions -> "strict" : true #8

cgers opened this issue Mar 11, 2019 · 2 comments

Comments

@cgers
Copy link

cgers commented Mar 11, 2019

Hi There

In typescript-express-example/src/index.ts you have some beautifully elegant code:

`
// register all application routes

AppRoutes.forEach(route => {

    app[route.method](route.path, (request: Request, response: Response, next: Function) => {

        route.action(request, response)

            .then(() => next)

            .catch(err => next(err));

    });

});

`

That registers all applications routes.

If you enable

"strict": true

in the tsconfig.json I get the error:

src/index.ts:19:4 - error TS7017: Element implicitly has an 'any' type because type 'Express' has no index signature. 19 app[route.method](route.path, (request: Request, response: Response, next: Function) => { ~~~~~~~~~~~~~~~~~

Is there a way to combine modules in this elegant way and enable strict?

Kind regards

@mrnkr
Copy link

mrnkr commented Sep 1, 2019

Well, this is awkward. Just started getting into typeorm today, really liking it, stumbled upon this question, tried the first solution that came to my mind and it worked! In case anyone will still find it useful I'll leave it here 🤓

The solution is to type AppRoutes as an array of Route. That Route interface has to look like this:

export interface Route {
  path: string;
  method: 'get' | 'post' | 'put' | 'delete';
  action:  (req: Request, res: Response) => Promise<void>;
}

This way you tell typescript exactly what strings to expect in that method key, hence it does not think it is viable for an invalid key to pop up there. I omitted the last argument for the action (next) on purpose, but do add it if you want it there!

Again, hope this helps someone! Happy coding!

@ScottWallace
Copy link

That is a wonderfully elegant piece of code, but it needs middleware, especially authentication middleware. Here's how to do it:

Add an element to the path objects that need authentication in the routes.ts file, called "secure". Not every element needs it.

  {
    path: '/users/user/prayers',
    method: 'post',
    action: GetUserPrayersAction
  },
  {
    path: '/users/user/settings/profileurl',
    method: 'post',
    action: SetUserProfilePictureAction,
    secure: true
  },

Now use this code in place of your standard AppRoutes.forEach:

  // register all application routes
  AppRoutes.forEach(route => {
    if (route.secure != true) {
      app[route.method](route.path, (request: Request, response: Response, next: Function) => {
        route.action(request, response)
          .then(() => next)
          .catch(err => next(err));
      });
    }
    else {
      app[route.method](route.path, firebaseService.authenticationMiddleware(), (request: Request, response: Response, next: Function) => {
        route.action(request, response)
          .then(() => next)
          .catch(err => next(err));
      });
    }
  });

You should replace firebaseService.authenticationMiddleware() with whatever middleware you want to provide.

Fun!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants