如何在 node/express 项目中使用明确类型的文件

How to use definitely typed files in node/express project

我从 tsd 和后来的类型开始,我们必须从这些来源安装并在服务器文件中提供参考,但现在我们可以使用 @types/filename 获取声明文件,我不知道为什么我们从 tsd 和打字转移到 @types。

如何将这些声明文件用于我的平均项目,我不知道如何导入或如何使它起作用并获得智能和类型检查。

我的项目中有 2 个文件夹,服务器和客户端,客户端有 angular 和所有前端的东西,服务器文件夹(关于节点)有 package.json、server.ts ,node_modules和tsconfig文件,这里是tsconfig文件和server.ts:它的抛出错误找不到模块@types/express

{
"compilerOptions": {
    "target": "ES5",
    "module": "commonjs",
    "typeRoots" : ["./node_modules/@types"],
    "moduleResolution": "node"
}
}

import * as express from "@types/express";
var app = express();

app.get('/', function (req, res) {
 res.send('Hello World!')
})

app.listen(3000, function () {
console.log('Example app listening on port 3000!')
})

这是package.json:

{
"name": "server",
"version": "1.0.0",
"description": "quiz application backend",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"@types/express": "^4.0.34",
"express": "^4.14.0"
}
}

使用 TypeScript 2,您只需通过 npm 安装类型:

npm install @types/lodash --save

并开始在您的 ts 文件中使用它:

import * as _ from "lodash";
_.padStart("Hello TypeScript!", 20, " ");

Intellisense 等可以工作,不需要旧的 /// <reference path="..." /> 语法。