Dylan Cristy
Dylan Cristy

Reputation: 984

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

I do a lot of work with SharePoint, so I would love to be able to install the SharePoint and Ajax typescript definitions files so I can have Intellisense for them in VS Code.

I found this other question here on SE:

How to Import Intellisense files into vsCode (Visual Studio Code)

but it deals with OSX (I'm on Windows), and apparently you still have to add references at the top of your code files.

Is there a way / place I can install them so that VS Code automatically loads them always and I do not have to reference them at the top of my code files?

Upvotes: 3

Views: 2345

Answers (3)

John Lee
John Lee

Reputation: 1141

From the docs: https://code.visualstudio.com/docs/languages/javascript#_automatic-type-acquisition

If you are using Visual Studio Code 1.8+, one alternative is to explicitly list packages to acquire type declaration files for in your jsconfig.json.

"typeAcquisition": {
    "include": [
        "lodash"
    ]
}

Upvotes: 0

Dylan Cristy
Dylan Cristy

Reputation: 984

Unfortunately there doesn't seem to be a way to do what I originally wanted, which is have the type definitions loaded whenever I started Code, no matter where I was working.

So the answer is to do something similar to what Nypan suggested, but it turns out it's even easier than that.

According to the documentation on the VS Code website, I don't even need a tsconfig.json file, I can use a jsconfig.json file, and I don't even need to specify the type definition files in the "files" section.

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.

So, I just have to have the .d.ts files somewhere under the "root" of my workspace, and a jsconfig.json file in the root that is as simple as:

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

and with that I get the IntelliSense for the SharePoint stuff.

Upvotes: 2

Nypan
Nypan

Reputation: 7246

You could always create a tsconfig.json file (read more about it here) and place it in the root of your "project".

Something like this:

{
    "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",
    ]
}

The above is just an example, read more about it at the link i provided.

Upvotes: 1

Related Questions