我正在编写一个 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 编译器看到它,但只有很多编译错误,因为它开始将所有子文件夹视为丢失的模块。