How to add TypeScript definition files to source control?

1.2k views Asked by At

I have simple asp.net core web application, where I have installed javascript libraries using libman.

I want to use typescript, so I have installed typescript definition files for the libraries using npm, e.g:

npm install @types/jquery --save-dev
npm install @types/bootstrap --save-dev

I would like to add the .d.ts files to source control, so that other developers does not have to rely on NPM - it is the purpose of libman, isn't it?

/node_modules folder is ignored in .gitignore by default.

How do I include the typescript definition files?

2

There are 2 answers

7
itminus On BEST ANSWER

Since you have installed javascript libraries using LibMan, you could simply reuse the LibMan to install the definitions too :

libman install @types/jquery -p unpkg
libman install @types/bootstrap -p unpkg

The default path will be libs/@types

lib/
    @types/
        bootstrap/
            index.d.ts
            ...
        jquery/
            index.d.ts
            ...

I create a tsconfig.json and configure path mapping to load modules as below :

{
    "compilerOptions": {
        "baseUrl": ".",
        "paths": {
            "jquery": ["lib/@types/jquery"] ,
            "bootstrap":["lib/@types/bootstrap"]
        }
      }
}

Now we can benefit from the typescript:

enter image description here

[Update]

For ASPNET-CORE project, the default path will be :wwwroot/lib/@types, if we have our tsconfig.json under the project directory (next to the *.csproj project file ), we need change the path to :

{
    "compilerOptions": {
        "baseUrl": ".",
        "paths": {
            "jquery": ["wwwroot/lib/@types/jquery"] ,
            "bootstrap":["wwwroot/lib/@types/bootstrap"]
        }
      }
}

enter image description here

0
kitsu.eb On

For those that would rather just type it in themselves: The JSON generated by libman install @types/jquery -p unpkg

{
    "provider": "unpkg",
    "library": "@types/[email protected]",
    "destination": "wwwroot/js/lib/@types/jquery"
}

(Note: I had an existing libman.json file, and this was added to the "libraries" array)