3
I’m trying to dynamically generate my application routes with the Express.router()
using Typescript and Node, but it doesn’t work and I’m not able to understand why.
If anyone please knows what is going on give me an explanation about or somewhere I can consult, I did not find anything similar in the documentation =(
router[route.method](route.path, route.handler, route.middleware)
The above code works normally on Node.js but in Typescript the following error occurs:
Element implicitly has an 'any' type because Expression of type 'string' can’t be used to index type 'Router'.
No index Signature with a Parameter of type 'string' was found on type 'Router'.
But n has another way, without disabling this option in tsconfig.json ?
– Renan Wallace
That option
--noImplicitAny
is not suitable for connecting libraries JS. Use it when using pure code only on Typescript.– Augusto Vasques
Another option would be to use a cast explicit to
any
only inrouter[route.method as any]
, so it is not necessary to disable the option for the whole project.– Luiz Felipe
@Luizfelipe Tested on the playground continues not supporting.
– Augusto Vasques
Really, you should have tested it before commenting, hehe. If you do the cast in the instance of router works:
(router as any)[route.method]
.– Luiz Felipe