Lie*_*ero 6 visual-studio typescript asp.net-core libman
当我使用 npm 安装打字稿定义时,我可以在作为 Visual Studio 构建一部分的打字稿文件中使用它们:
npm install @types/jQuery
<TypeScriptCompile Include="wwwroot\js\site.ts" />
到 .csproj。$
在打字稿文件中使用全局变量但是,当我使用 libman 下载打字稿定义时,我在配置打字稿构建以使用它们时遇到了麻烦。
我想我需要添加 tsconfig.json,但它似乎干扰了 csproj 中的 TypeScriptCompile。
wwwroot
js
site.d.ts
site.ts
lib
jquery
jquery.min.js
@types
jquery
index.d.ts
misc.d.ts
…
MyProject.csproj
libman.json
Run Code Online (Sandbox Code Playgroud)
如何修改项目(.csproj、tsconfig.json),以便使用 wwwroot\lib\@types 而不是 node_modules/@types 中的定义文件和全局变量?
我已经创建了 tsconfig.json 并设置
"compilerOptions": {
"typeRoots": [ "wwwroot/lib/@types" ]
}
Run Code Online (Sandbox Code Playgroud)
但现在构建会在 site.ts 中返回诸如“找不到名称 MyInterface”之类的错误。MyInterface 在 site.d.ts 中定义。为什么在没有 tsconfig.json 时可以识别类型,而现在不能识别?我想避免添加///<reference>
评论
我最终将 site.d.ts 重命名为 global.d.ts(感谢@itminus answer),并在 tsconfig 中明确设置 popper.js 模块的路径:
{
"compileOnSave": true,
"compilerOptions": {
"baseUrl": "./",
"paths": {
"popper.js": [ "wwwroot/lib/popper.js" ] //use this instead of wwwroot/lib/@types/popper.js which is incompatible
},
"typeRoots": [ "wwwroot/lib/@types" ]
},
"include": [ "wwwroot/js/**/*.ts" ]
}
Run Code Online (Sandbox Code Playgroud)
popper.js 是 bootstrap 4 的依赖项。这有点棘手,因为@types/bootstrap
它依赖于popper.js
而不是@types/popper.js
(这是不兼容的并且会在 @types/bootstrap/index.d.ts 中抛出 typescript 错误);
幸运的是,popper.js 包含打字稿定义(index.d.ts),我只需要告诉打字稿编译器在哪里查看它。
归档时间: |
|
查看次数: |
2734 次 |
最近记录: |