简体   繁体   English

如何将TypeScript定义文件添加到源代码管理中?

[英]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. 我有一个简单的asp.net核心Web应用程序,其中已使用libman安装了javascript库。

I want to use typescript, so I have installed typescript definition files for the libraries using npm, eg: 我想使用打字稿,所以我已经使用npm为库安装了打字稿定义文件,例如:

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? 我想将.d.ts文件添加到源代码管理中,以便其他开发人员不必依赖NPM-这是libman的目的,不是吗?

/node_modules folder is ignored in .gitignore by default. 默认情况下,.gitignore中将忽略/ node_modules文件夹。

How do I include the typescript definition files? 如何包含打字稿定义文件?

Since you have installed javascript libraries using LibMan , you could simply reuse the LibMan to install the definitions too : 由于您已经使用LibMan安装了javascript库,因此您也可以简单地重用LibMan来安装定义:

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

The default path will be libs/@types 默认路径为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 : 我创建一个tsconfig.json并配置路径映射以加载模块,如下所示:

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

Now we can benefit from the typescript: 现在我们可以从打字稿中受益:

在此处输入图片说明

[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 : 对于ASPNET-CORE项目,默认路径为: wwwroot/lib/@types ,如果我们在项目目录( *.csproj项目文件旁边)下有tsconfig.json ,则需要将路径更改为:

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

在此处输入图片说明

For those that would rather just type it in themselves: The JSON generated by libman install @types/jquery -p unpkg 对于那些只想自己输入的人: libman install @types/jquery -p unpkg生成的JSON libman install @types/jquery -p unpkg

{
    "provider": "unpkg",
    "library": "@types/jquery@3.3.29",
    "destination": "wwwroot/js/lib/@types/jquery"
}

(Note: I had an existing libman.json file, and this was added to the "libraries" array) (注意:我有一个现有的libman.json文件,并且已将其添加到"libraries"数组中)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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