【发布时间】:2019-01-12 02:03:38
【问题描述】:
我正在尝试使用 typescript 在 node 中运行 Hello World express 应用程序,而 DevelopedlyTyped 类型似乎被完全忽略了。
package.json:
{
"dependencies": {
"@types/express": "*",
"@types/node": "*",
"express": "*"
}
}
tsconfig.json:
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es2015"
],
"target": "es6",
"noImplicitAny": true,
"strictNullChecks": true,
"noImplicitThis": true,
"noImplicitReturns": true,
"moduleResolution": "node",
"outDir": "dist",
"baseUrl": "."
},
"include": [
"src/**/*"
]
}
src/app.ts:
const express = require('express'); //express is typed "any" because @types/express is apparently ignored
const app = express();
const port = 3000;
app.get('/', (req, res) => res.send('Hello World!')); //Compile error here because implicit any
app.listen(port, () => console.log(`Example app listening on port ${port}!`));
编译输出:
~/tmp/ws-test$ tsc
src/app.ts(5,15): error TS7006: Parameter 'req' implicitly has an 'any' type.
src/app.ts(5,20): error TS7006: Parameter 'res' implicitly has an 'any' type.
我觉得我正在服用疯狂的药丸。这里有什么问题?
【问题讨论】:
-
你能显示构建命令和输出吗?
-
试试这个:import * as express from 'express';
-
Juraj,你赢了,成功了。看起来那些应该是相同的陈述,对吧?是否存在一些我不知道的语义差异?
-
当我使用 require 而不是新的 es 导入时我没有类型,不知道为什么但我从未尝试过解决,我一直在使用 import...
标签: node.js typescript express definitelytyped