如何安装 TypeScript 定义文件,以便它始终由 VS Code 加载?

How can I install a TypeScript definition file so it is always loaded by VS Code?

我用 SharePoint 做了很多工作,所以我很想能够安装 SharePoint and Ajax typescript definitions 文件,这样我就可以在 VS Code 中为它们提供 Intellisense。

我在 SE 上发现了另一个问题:

但它涉及 OSX(我在 Windows),显然您仍然需要在代码文件的顶部添加引用。

有没有什么方法/地方可以安装它们,以便 VS Code 始终自动加载它们,而不必在我的代码文件的顶部引用它们?

您始终可以创建一个 tsconfig.json 文件 (read more about it here) 并将其放在 "project".

的根目录中

像这样:

{
    "compilerOptions": {
        "module": "commonjs",
        "noImplicitAny": true,
        "removeComments": true,
        "preserveConstEnums": true,
        "out": "../../built/local/tsc.js",
        "sourceMap": true
    },
    "files": [
        "myDefinition.d.ts",
        "myOtherDefinition.d.ts",
        "SomethingToTranspile.ts",
        "SomethingElseToTranspile.ts",
    ]
}

以上只是一个示例,请在我提供的 link 中阅读更多相关信息。

不幸的是,似乎没有办法做我最初想要的,无论我在哪里工作,只要我启动 Code,就会加载类型定义。

所以答案是做类似于 Nypan 建议的事情,但事实证明它比那更容易。

根据documentation on the VS Code website,我什至不需要tsconfig.json文件,我可以使用jsconfig.json文件,我什至不需要指定类型"files" 部分中的定义文件。

As an example, you can just drop a new type definition .d.ts file into your project folder and VS Code will pick it up automatically.

所以,我只需要在我的工作空间的 "root" 下的某处放置 .d.ts 文件,并在根目录中放置一个 jsconfig.json 文件,它很简单:

{
    "compilerOptions": {
        "module": "commonjs"
    }
}

然后我获得了 SharePoint 内容的 IntelliSense。

来自文档:https://code.visualstudio.com/docs/languages/javascript#_automatic-type-acquisition

如果您使用的是 Visual Studio 代码 1.8+,一种替代方法是在 jsconfig.json.

中显式列出要获取类型声明文件的包
"typeAcquisition": {
    "include": [
        "lodash"
    ]
}