typescript - 在 ASP.NET Core 应用程序中编译打字稿时如何包含引导表的类型
问题描述
我正在编写一个 ASP.NET Core 5 应用程序,而不是使用 JS,我想使用 TypeScript。
我正在使用一些 JS 库,所以为了让 TS 编译它需要为它们定义。
我已经使用库的类型定义的 devDependencies 设置了 NPM,它将它们下载到 node_modules,TS 编译器会找到并使用它们。
我的 NPMpackage.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>'.
我认为这个问题可能是由于引导表的类型定义包含在主库中,这意味着它不在 node_modules 的 @types 子目录中。
我尝试通过手动指定 typeRoots 来强制 TS 编译器查看它,但是只有很多编译错误,因为它开始将所有子文件夹视为缺少的模块。
解决方案
推荐阅读
- angular - 关于在 Angular 项目中使用 async/await 的注意事项
- r - 如何从 r-markdown pdf 中删除标题?
- asp.net-core - 使用 C# 禁用 Chrome 最小化按钮
- spring-boot - 如何在 Spring Boot 中插入自定义 PropertySource 提供的属性值?
- flutter - 是否可以在颤振库中为 Moor 实现“数据库优先”的方法?
- java - 如何使用带有 java 11 http 客户端的流编写请求正文?
- regex - 使用 ansible.builtin.lineinfile 在块的末尾插入行
- javascript - 在电子的渲染器进程中使用 npm 模块
- javascript - VueJS:如何在不影响观察者的情况下更新观察者内部的值
- git - GitLab中合并训练和合并结果有什么区别