关于 TS 2.x 的一切 @types
看起来很棒,但我一辈子都想不出如何让它正常工作!
- 我安装了 Visual Studio 2015 - 版本
14.0.25431.01 Update 3
- 我有 TypeScript
2.1.4
对于已安装的 Visual Studio 2015,which I got from here - VS Web 项目已设置为使用 TypeScript 2.1
<TypeScriptToolsVersion>2.1</TypeScriptToolsVersion>
这是我的 packages.json
的相关部分文件
"dependencies": {
"angular": "^1.5.9",
"angular-messages": "^1.5.9",
"angular-ui-bootstrap": "^2.3.0",
"angular-ui-router": "^0.3.2",
"moment": "^2.17.0",
"underscore": "^1.8.3"
},
"devDependencies": {
"@types/angular": "^1.5.21",
"@types/angular-ui-bootstrap": "^0.13.36",
"@types/angular-ui-router": "^1.1.35",
"@types/jquery": "^2.0.34",
"@types/node": "^0.0.3",
"@types/signalr": "^2.2.32",
"@types/underscore": "^1.7.36"
}
这是我的完整 tsconfig.json
文件
{
"compilerOptions": {
"module": "commonjs",
"noImplicitAny": true,
"removeComments": true,
"sourceMap": true,
"target": "ES5"
},
"typeAcquisition": {
"enable": true
}
}
我还尝试了 typeRoots
的变体和 types
在 compilerOptions
中指定(一个,另一个,两者,两者都不是) , 但没有运气!
"typeRoots": [
"./node_modules/@types"
],
"types": [
"angular",
"angular-ui-bootstrap",
"angular-ui-router",
"jquery",
"moment",
"signalr",
"underscore"
]
我已经清理了构建、重新启动了 Visual Studio 等,但无论我做什么,我只会遇到类似这样的构建错误
some-file.ts(8,22): error TS2304: Build:Cannot find name 'angular'.
some-file.ts(12,41): error TS2694: Build:Namespace 'angular' has no exported member 'IScope'.
some-file.ts(12,67): error TS2694: Build:Namespace 'angular' has no exported member 'IRootElementService'.
another-file.ts(26,22): error TS2503: Build:Cannot find namespace 'moment'.
another-file.ts(47,37): error TS2304: Build:Cannot find name 'moment'.
所有 typedef 都存在于磁盘上 node_modules/@types
中或与相关包本身。我不知道为什么 Visual Studio/TypeScript 找不到这些文件!我觉得有些东西要么还没有准备好发布,要么我错过了一些非常简单的东西。请有人在这里指出我正确的方向
最佳答案
如果您还没有解决这个问题,请尝试从卸载/安装程序中卸载 typescript 工具(您可能像我一样有多个版本)并通过您已有的安装包重新安装 typescript 工具 @ v2.1.4。在 visual studio 中仔细检查你正在使用的版本(它应该在帮助菜单中的“关于 microsoft visual studio”中有详细信息)
这是我的 tsconfig 以防有帮助:
{
"compilerOptions": {
"target": "es5",
"module": "system",
"moduleResolution": "node",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"removeComments": false,
"noImplicitAny": false,
"typeRoots": [
"node_modules/@types"
]
},
"typeAcquisition": {
"enable": true
},
"exclude": [
"jspm_packages"
]
}
这适用于导出模块的任何类型,但是当它们不是模块(即只是接口(interface)等)时导入单个类型时我仍然遇到问题......
一般来说,我不必为库执行任何 import * from x
语句,并且能够只使用直接导出的模块的名称。
关于visual-studio - 使用 TypeScript 2.1 使 @types 在 Visual Studio 2015 Update 3 中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41113136/