gittypescriptasp.net-corenpmlibman

How to add TypeScript definition files to source control?


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?


Solution

  • 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