Definitelytyped: @types/express - app.use() not working as expected

Created on 19 May 2017  路  2Comments  路  Source: DefinitelyTyped/DefinitelyTyped

Using the express @types/express

  • Authors: @borisyankov

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?

Most helpful comment

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.

All 2 comments

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!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

lilling picture lilling  路  3Comments

fasatrix picture fasatrix  路  3Comments

ArtemZag picture ArtemZag  路  3Comments

victor-guoyu picture victor-guoyu  路  3Comments

csharpner picture csharpner  路  3Comments