简体   繁体   中英

How to use Typescript Definition files in Visual Studio 2017?

I have an ASP.NET Core 2.0 app that I'm trying to use TypeScript on, and it works, but when I try to use jQuery it doesn't give me any intellisense. Simple enough I thought, I just need to add the definition file and all will be well. Except, none of the ways I've tried seem to work.

Adding the DefinitellyTyped nuget package does nothing, and in-fact, I can't see where it's being placed, unlike in VS2013. Adding the NPM packages "jquery" and "jquery-ts" does nothing. The only thing that has worked was for me to manually add the definition file from another project, but that's just too much work to have to do whenever there's updates.

So, what is the proper way to get TS definition files to work for libraries I want to use in my project? Not sure if it matters, but I'm using Gulp in my project.

I pull in all of my type definitions using NPM, in association with a package.json file.

The below will result in the jQuery types (and jQuery itself) being pulled into the node_modules folder when you npm install in your project folder. You can use a Gulp task to move files you need for real from node_modules into your public folder (ie if you want to get jQuery via NPM).

{
    "name": "your-project",
    "private": true,
    "devDependencies": {
      "@types/jquery": "^3.2.16"
    },
    "dependencies": {
      "jquery": "^3.2.1"
    }
}

NPM is the de-facto way to grab types, and all the Definitely Typed ones are inside the `@types organisation.

Visual Studio plays nicely with NPM now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM