【问题标题】:Getting @types to work in Visual Studio 2015 Update 3 with TypeScript 2.1使用 TypeScript 2.1 让 @types 在 Visual Studio 2015 Update 3 中工作
【发布时间】:2017-04-28 01:03:21
【问题描述】:

关于 TS 2.x @types 的一切看起来都很棒,但我终生无法弄清楚如何让它正常工作!

  • 我已安装 Visual Studio 2015 - 版本 14.0.25431.01 Update 3
  • 我已经安装了用于 Visual Studio 2015 的 TypeScript 2.1.4which I got from here
  • VS Web 项目已设置为使用带有<TypeScriptToolsVersion>2.1</TypeScriptToolsVersion> 的 TypeScript 2.1

这是我的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
  }
}

我也尝试过在compilerOptions 中指定typeRootstypes(一个、另一个、两个、一个都不)的变体,但没有运气!

    "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'.

所有类型定义都存在于磁盘上的node_modules/@types 或相关包本身中。我不知道为什么 Visual Studio/TypeScript 找不到这些文件!我觉得有些东西要么还没有准备好发布,要么我错过了一些非常简单的东西。请有人在这里指出我正确的方向

【问题讨论】:

    标签: visual-studio typescript typescript-typings typescript2.0 typescript-types


    【解决方案1】:

    如果您还没有解决这个问题,请尝试从卸载/安装程序中卸载 typescript 工具(您可能像我一样有多个版本)并通过您已有的安装包重新安装 typescript tools @ 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"
      ]
    }
    

    这适用于任何导出模块的类型,但在导入不是模块(即只是接口等)的单个类型时我仍然遇到问题...

    通常我不必为库执行任何import * from x 语句,并且能够直接使用直接导出的模块的名称。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-28
      • 2017-02-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多