如何安装自定义类型? [英] How to install custom typings?
问题描述
我创建了一个自定义类型,其中包含我想在多个 webstorm node.js 项目中共享的代码.问题是我试图找到概述如何在项目中包含输入的文档.我尝试使用 npm 命令,但它没有将该文件夹添加到/node_modules 文件夹下的 @typings 文件夹中.此外,当我编译我试图添加自定义类型的项目时,我在包含类型的项目和我想要添加类型的项目之间收到 mongoose 库的重复错误.我不确定可能是什么问题.
I created a custom typing contains code that I would like to share across several webstorm node.js projects. The issue is that I am trying to find documentation outlining how to include the typing in a project. I tried to using the npm command but it did not add the folder to the @typings folder under /node_modules folder. Also, when I compile the project that I am trying to add the custom typing to, I am getting duplicate errors for the mongoose library between the project that contains the typing and the project I would like to add the typing to. I am not sure of what the problem could be.
tsconfig.json(对于新类型):
tsconfig.json(for the new type):
{
"name": "newcustomtype",
"description": "...",
"version": "1.0.0",
"main": "./dist/index.js",
"typings": "./dist/index.d.ts",
"scripts": {
"grunt": "grunt"
},
"dependencies": {
"@types/express": "^4.0.35",
"@types/express-jwt": "0.0.34",
"debug": "^2.2.0",
"dotenv": "^4.0.0",
"mongoose": "^4.9.2",
"tslint": "^4.5.1"
},
"devDependencies": {
"@types/mongodb": "^2.1.41",
"@types/mongoose": "^4.7.9",
"@types/node": "^7.0.10",
"grunt": "^1.0.1",
"grunt-cli": "^1.2.0",
"grunt-contrib-watch": "^1.0.0",
"grunt-ts": "^6.0.0-beta.15",
"grunt-tslint": "^4.0.1",
"nodemon": "^1.11.0",
"ts-node": "^3.0.2",
"tslint": "^4.5.1",
"typescript": "^2.2.1"
}
}
tsconfig.json(应该安装打字的地方):
tsconfig.json(where the typing should be installed):
{
"compilerOptions": {
"module": "commonjs",
"target": "ES5",
"moduleResolution": "node",
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"types": ["reflect-metadata"],
"lib": ["ES6"],
"sourceMap": true,
"inlineSources": true,
"pretty": true,
"outDir": "dist",
"rootDir": "src",
"noLib": false
},
"include": [
"src/**/*.ts"
],
"exclude": [
"node_modules"
],
"compileOnSave": false
}
我试图按照文档进行操作.在打字稿网站上,但我无法找到概述一旦创建后如何安装的资源.虽然,为了不安装自定义类型,我认为我的 tsconfig 文件也有问题.请查看并让我知道我缺少什么?
I tried to follow the doc. on the typescript website but I have not been able to find a resource outlining how to install once created. Although, for it to not install the custom typing, I think there is also a problem with my tsconfig files as well. Please review and let me know what I am missing?
提前致谢.
推荐答案
node_modules/@types
文件夹仅用于由 npm 安装的定义.对于自定义类型,最好 指定 <tsconfig.json
中的 code>typeRoots.例如:
The node_modules/@types
folder is only for definitions which get installed by npm. For custom typings it would be best to specify typeRoots
within your tsconfig.json
. For example:
{
[...]
"compilerOptions": {
[...]
"typeRoots" : ["../path/to/typings", "./node_modules/@types"],
[...]
}
[...]
}
这篇关于如何安装自定义类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!