【问题标题】:Node types not working in typescript节点类型在打字稿中不起作用
【发布时间】:2018-04-30 02:12:24
【问题描述】:

我已经包含了一个 Typescript 定义文件,这样我就可以在 Typescript 中编写我非常基本的 node express 服务器。我已经安装了这些类型:

npm install @types/node --save-dev

遗憾的是,我的 IDE(VS 代码)仍然无法识别节点类型:

server.ts

import * as express from "express";

const app = express();

app.get('/', (req, res) => {
  res.send('Hello Typescript!')
});

const server = app.listen(3000, () => {
  console.log("listening on port 3000")
})

错误

app.get“函数”类型上不存在属性“get”

app.listen“功能”类型上不存在“侦听”属性

【问题讨论】:

    标签: node.js typescript


    【解决方案1】:

    您还需要 Express 类型:

    npm install @types/express --save-dev

    【讨论】:

    • 这消除了错误,编译器工作,但我没有得到自动完成。 express 被视为any 并且键入app. 并不表示listenget。这是预期的行为吗?没有类型检查就没有理由使用 typecipt...
    猜你喜欢
    • 2018-03-11
    • 2019-03-16
    • 2016-03-06
    • 2015-08-17
    • 1970-01-01
    • 1970-01-01
    • 2019-12-19
    • 1970-01-01
    • 2016-10-15
    相关资源
    最近更新 更多