Using the express @types/express
Thanks for you work Boris! I've been trying to read through the code to understand why I am getting this error:
For the first parameter in express.use() function, I am getting this error:
Argument of type 'string' is not assignable to parameter of type 'RequestHandlerParams'.
App
import * as express from 'express'
class App {
express: express.Application
constructor(){
this.express = express()
this.routes()
}
private routes(){
let routers: Array<Route> = [new IndexRoute]
routers.forEach(route => {
// Problem is showing here on the first argument being called with route.url
this.express.use(route.url, (req: Request, res: Response, next: Function) => {
})
})
}
}
Route v1
import * as express from 'express'
export class Route {
routehandler: (req: Request, res: Response, next: Function) => {}
url: string
}
I have also tried this:
Route v2
import * as express from 'express'
export class Route {
routehandler: express.Router
url: string
constructor(){
this.routehandler = express.Router()
}
}
IndexRoute
import * as express from 'express'
import { Route } from "./Route";
export class IndexRoute extends Route {
constructor() {
super()
this.url = '/'
}
}
In javascript you can create new Routers and pass them to the express app to be used on a certain route like so:
const express = require('express')
let app = express()
let router = express.Router()
app.use('/newroute', router)
Is this intentional, am I doing something wrong?
Looks like you have the wrong Request and Response types. The types used by the handlers passed to use are declared by the express declaration but you are not qualifying them.
Request --> express.Request
Also, avoid using the type Function.
Seems to have resolved my issue. Thanks @aluanhaddad!
Most helpful comment
Looks like you have the wrong
RequestandResponsetypes. The types used by the handlers passed touseare declared by the express declaration but you are not qualifying them.Request-->express.RequestAlso, avoid using the type
Function.