表达app.get函数给出"参数类型与参数不匹配"错误

时间:2016-10-25 05:58:20

标签: typescript ide webstorm typescript-typings typescript2.0

以下代码......

app.get('/basic', (req, res) => {
    res.send({message: 'hello'})
})

在WebStorm 2016.2.4中生成消息Argument types do not match parameters

Argument types do not match parameters

我的package.json中的相关依赖项部分是:

"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"
},

当我删除@types/express包时,WebStorm不再提供错误消息,但TypeScript编译器会发出error TS2307: Cannot find module 'express'消息。

有没有办法以不同的方式配置它,或者WebStorm只是在追赶TypeScript 2?

2 个答案:

答案 0 :(得分:2)

有同样的问题。问题是我通过typings install dt~express安装了dt / express的输入,当我在express / index.ts文件中查看.get()的类型定义时,出现了不匹配的情况:

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

这不是我想要的,我改为使用typings install express从npm / express获取类型定义。

我的typings.json文件如下所示:

{
"dependencies": {
    "express": "registry:npm/express#4.14.0+20160925001530"
  },
...
}

答案 1 :(得分:0)

编辑:这在Webstorm v2016.3.1中解决了

在此之前,我发现的部分解决方案是以下列方式添加类型声明:

import * as express from 'express'

const app : express.Application | express.Router = express()
相关问题