当前位置: 动力学知识库 > 问答 > 编程问答 >

typescript - express app.get function gives "Argument types do not match parameters" error

问题描述:

The following code...

app.get('/basic', (req, res) => {

res.send({message: 'hello'})

})

generates a message Argument types do not match parameters in WebStorm 2016.2.4

The relevant dependencies section in my package.json is:

"dependencies": {

"@types/body-parser": "0.0.32",

"@types/express": "^4.0.33",

"@types/lodash": "^4.14.34",

"@types/node": "^6.0.38",

"body-parser": "1.15.1",

"dotenv": "2.0.0",

"express": "4.13.4",

"lodash": "^4.13.1",

"typescript": "^2.0.3"

},

When I remove the @types/express package, WebStorm no longer gives the error message, but the TypeScript compiler gives a error TS2307: Cannot find module 'express' message.

Is there a way to configure this differently or is WebStorm just playing catch-up with TypeScript 2?

网友答案:

Had the same issue. Problem is I installed the typings from dt/express via typings install dt~express and when I looked at the type definition of .get() at the express/index.ts file, there was a mismatch:

get: {(name: string): any;} & IRouterMatcher<this>;

This was not what I wanted, and I instead did a typings install express to get the type definitions from npm/express.

My typings.json file looks like this:

{
"dependencies": {
    "express": "registry:npm/express#4.14.0+20160925001530"
  },
...
}
网友答案:

TypeScript is unable to find declarations of inner objects properly - Instead of importing express in it entirety and accessing its subobjects, e.g.:

import * as express from "express";
...
public app: express.Application;

Do

import { NextFunction, Application, Router } from "express";
...
public app: Application;
网友答案:

EDIT: This solved itself in Webstorm v2016.3.1


Before that, a partial solution I had discovered was adding type declarations in the following fashion:

import * as express from 'express'

const app : express.Application | express.Router = express()
分享给朋友:
您可能感兴趣的文章:
随机阅读: