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

How to add TypeScript definition files to source control?

我有一个简单的 asp.net 核心 Web 应用程序,其中我使用 libman 安装了 javascript 个库。

我想使用 typescript,所以我使用 npm 为库安装了 typescript 定义文件,例如:

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

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

/node_modules 文件夹默认在 .gitignore 中被忽略。

如何包含打字稿定义文件?

因为您已经使用 LibMan 安装了 javascript 库,您可以简单地重用 LibMan 来安装定义:

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

默认路径为libs/@types

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

我创建一个 tsconfig.json 并配置路径映射以加载模块,如下所示:

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

现在我们可以从打字稿中受益了:

[更新]

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

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

对于那些只想自己输入的人:libman install @types/jquery -p unpkg

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

(注意:我有一个现有的 libman.json 文件,并将其添加到 "libraries" 数组)