在 ASP.NET Core 应用程序中编译打字稿时如何包含引导表的类型

我正在编写一个 ASP.NET Core 5 应用程序,我想使用 TypeScript,而不是使用 JS。

我正在使用一些 JS 库,所以为了让 TS 编译它需要为它们定义。

我已经使用库的类型定义的 devDependencies 设置了 NPM,它将它们下载到 node_modules,然后 TS 编译器找到并使用它们。

我的 NPM package.json 是:

{
    "version": "1.0.0","name": "asp.net","private": true,"devDependencies": {
        "@types/bootstrap": "5.0.17","@types/jquery": "3.5.6","bootstrap-table": "^1.18.3","jquery": "3.6.0"
    }
}

我的 tsconfig.json 是:

{
    "compilerOptions": {
        "noImplicitAny": true,"noEmitOnError": true,"removeComments": true,"sourceMap": true,"target": "es5","lib": [
            "DOM","ES5","ES2015"
        ]
    },"include": [
        "wwwroot/js/**/*.ts"
    ],"exclude": [
        "node_modules"
    ],"compileonSave": true
}

这适用于 JQuery 和 Bootstrap,但不适用于 bootstrap-table。

我的项目结构是:

root
  wwwroot
    js
      home.ts
  views
    home
      index.cshtml      // imports ~/js/home.js
  node_modules          // the folders in here are automatically maintained by NPM
    @types
      bootstrap
      jquery
    bootstrap-table
  package.json
  tsconfig.json

编译home.ts时的错误是(TS) Property 'bootstrapTable' does not exist on type 'JQuery<HTMLElement>'.

我认为这个问题可能是由于 bootstrap-table 的类型定义包含在主库中引起的,这意味着它不在 node_modules 的 @types 子目录中。

我已经尝试通过手动指定 typeRoots 来强制 TS 编译器看到它,但只有很多编译错误,因为它开始将所有子文件夹视为丢失的模块。

abk168 回答:在 ASP.NET Core 应用程序中编译打字稿时如何包含引导表的类型

是的,如果你只是想使用bootstrap-table插件,可以直接使用add -client库下载插件: enter image description here

或者直接使用CDNjs节点:

https://cdnjs.com/libraries/bootstrap-table

,

$AIRFLOW_HOME/logs/scheduler/[date]/[yourdagfile.py].log 不包含 @types/bootstrap 的类型,因为它“只是”一个插件,我找不到它的类型定义(仅适用于 react)。

您的选择是:

  • 找到另一个具有 Typescript 类型定义的库(可能不是您想要的)
  • 为 bootstrap-table 编写您自己的 d.ts 文件(这是一项艰巨的任务 - start here;如果您这样做,请将其添加到 DefinitelyTyped github 存储库)
  • 现在,我建议您首先添加一个 (d.)ts 文件(例如 main.ts),而不为您的类型定义添加 bootstrap-tableimport 语句,并“扩展”{{ 1}} 接口,示例如下(这很简单,但如果您在此文件中扩展其他 jquery 插件可能会变得丑陋)

示例:

export

参考:

Extending jquery for bootstrap support

Thisthis SO 答案

本文链接:https://www.f2er.com/3872.html

大家都在问