gpt4 book ai didi

node.js - 使用 TypeScript 中的类型导入 Node 和表达

转载 作者:IT老高 更新时间:2023-10-28 22:04:15 32 4
gpt4 key购买 nike

我正在尝试在 Visual Studio Code 中按照 Microsoft guide 设置 TypeScript express/node 应用程序。但是将其更改为使用 TypeScript 但是在使用 typings 安装类型定义时,我似乎必须安装比指南更多的包。

我正在运行以下一对命令:

typings install node --ambient --save
typings install express --ambient --save

但是尝试仅使用这些包进行构建会出现以下类型的错误:

error TS2307: Cannot find module 'serve-static'.

对于以下类型:

  • mime
  • express-serve-static-core
  • 静态服务

我可以通过安装所需的类型来解决这个问题,但似乎类型应该自己做。

我想检查我是否遗漏了自动引入依赖项的基本步骤,或者该指南是否已过时?

如果相关,我的 tsconfig.json:

{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"sourceMap": true,
"outDir": "bin",
"sourceRoot": "src"
},
"exclude": [
"node_modules",
"typings/browser.d.ts",
"typings/browser"
]
}

我的 tsc 是 1.8.7 版,我在全局范围内安装了 typescript。

最佳答案

截至上个月TypeScript 2.0 发布,推荐的安装打字工具是我们值得信赖的老 friend npm而不是 typingstsd .

npm install @types/node --save

使用 npm,无需再担心“全局”或“环境”安装。

您也不必担心添加 <reference>不再将标签放在源文件的顶部;只需将以下属性放入您的 compilerOptionstsconfig.json ,TypeScript 编译器会自动找到你安装的 npm 类型:

"typeRoots": [ "node_modules/@types" ]

这里有一篇博文更详细地解释了这一变化:https://blogs.msdn.microsoft.com/typescript/2016/06/15/the-future-of-declaration-files/

关于node.js - 使用 TypeScript 中的类型导入 Node 和表达,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36013174/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com