如何在 Visual Studio 2015 中为 Typescript 添加一个打字到 typings.json?

How does one add a typing to typings.json for Typescript in Visual Studio 2015?

我不得不问,因为这让我发疯。我看到在 Google 上安装 typings 的 npm 方式,但是 Angular2's tutorial 有一个添加 typings.json 文件然后它添加了 typings 文件夹并自动从 DefinitelyTyped 下载 d.ts 文件。我用 jquery 试过了,但没有下载。我还尝试重建项目,我希望 package.json 包含添加额外类型的命令。

这是我在 package.json 文件中的脚本:

"scripts": {
  "start": "tsc && concurrently \"npm run tsc:w\" \"npm run lite\" ",
  "tsc": "tsc",
  "tsc:w": "tsc -w",
  "lite": "lite-server",
  "typings": "typings",
  "postinstall": "typings install"
}

这是我试过的 typings.json 文件。下载 es6-shim 和 jasmine。

{  "ambientDependencies": {
   "es6-shim": "github:DefinitelyTyped/DefinitelyTyped/es6-shim/es6-shim.d.ts#7de6c3dd94feaeb21f20054b9f30d5dabc5efabd",
   "jasmine": "github:DefinitelyTyped/DefinitelyTyped/jasmine/jasmine.d.ts#7de6c3dd94feaeb21f20054b9f30d5dabc5efabd",
   "jquery": "github:DefinitelyTyped/DefinitelyTyped/jquery/jquery.d.ts#7de6c3dd94feaeb21f20054b9f30d5dabc5efabd"
}}

这可能很简单,比如在主题标签后没有看似校验和的内容。我在哪里可以找到正确的校验和,或者我需要将什么命令添加到 package.json 以在编译时检索类型,或者我做错了什么?

Here's another example 添加一行到 typings.json 文件,然后它会为您安装 d.ts 文件。向下滚动,直到看到 手动输入

  1. 确保您已安装 npm
  2. 打开您选择的控制台(例如命令提示符或 powershell)
  3. 导航到您的项目文件夹

仅使用 npm(TypeScript 2 及更高版本):

  1. npm install --save @types/jquery

    完成:有关详细信息,请参阅 this

使用typings(v.2 之前的打字稿):

  1. 确保你已经安装了 typings,如果没有安装 运行 npm install typings --global
  2. 写入typings install dt~jquery --save --global

    这应该会更新您的 typings.json 文件并下载定义文件。

    在上面的打字示例中,'dt~' 表示它应该在 DefinitelyTyped 存储库中查找 jquery,默认为 'npm'。语法从版本 0.x 到 1.0 略有变化,标志 --global 以前是 --ambient.

我发现在 typings.json 文件创建后打开并重新保存 package.json 会触发输入下拉。这是我当前的打字文件:

{
    "ambientDependencies": {
        "es6-shim": "github:DefinitelyTyped/DefinitelyTyped/es6-shim/es6-shim.d.ts#7de6c3dd94feaeb21f20054b9f30d5dabc5efabd",
        "jasmine": "github:DefinitelyTyped/DefinitelyTyped/jasmine/jasmine.d.ts#7de6c3dd94feaeb21f20054b9f30d5dabc5efabd"
  }
}