问题描述
当我使用npm安装打字稿定义时,可以在属于Visual Studio构建的打字稿文件中使用它们:
When I install typescript definitions using npm, I can use them in typescript files that are part of Visual Studio build:
- 在Visual Studio中创建新的ASP.NET Core Web应用程序
- 使用libman添加jQuery
- 使用
npm install @types/jQuery
添加jQuery - 创建新的打字稿文件:wwwroot/js/site.ts.这会将
<TypeScriptCompile Include="wwwroot\js\site.ts" />
添加到.csproj. - 我现在可以在打字稿文件中使用
$
全局变量
- Create new ASP.NET Core Web Application in Visual Studio
- Add jQuery using libman
- Add jQuery using
npm install @types/jQuery
- Create new typescript file: wwwroot/js/site.ts. This adds
<TypeScriptCompile Include="wwwroot\js\site.ts" />
to .csproj. - I can now use
$
global variable in my typescript files
但是,当我使用libman下载打字稿定义时,我很难配置打字稿版本以使用它们.
However, when I use libman to download the typescript definitions, I have troubles configuring the typescript build to use them.
我想我需要添加tsconfig.json,但这似乎会干扰csproj中的TypeScriptCompile.
I guess I need to add tsconfig.json, but it seems to interfere with the TypeScriptCompile in the csproj.
wwwroot
js
site.d.ts
site.ts
lib
jquery
jquery.min.js
@types
jquery
index.d.ts
misc.d.ts
…
MyProject.csproj
libman.json
如何修改项目(.csproj,tsconfig.json),以便使用wwwroot \ lib \ @types中的定义文件和全局变量而不是node_modules/@ types?
How do I modify the project (.csproj, tsconfig.json) so that I will work with the definition files and global variables in wwwroot\lib\@types instead of node_modules/@types?
我已经创建了tsconfig.json并设置了
I've created tsconfig.json and set
"compilerOptions": {
"typeRoots": [ "wwwroot/lib/@types" ]
}
,但是现在该构建在site.ts中返回诸如找不到名称MyInterface"的错误. MyInterface在site.d.ts中定义.为什么在没有tsconfig.json但现在没有tsconfig.json时可以识别类型?我想避免添加///<reference>
评论
but now the build returns errors like "Cannot find name MyInterface" in site.ts. The MyInterface is defined in site.d.ts. Why the type was recognized when there was no tsconfig.json and now it doesn't? I want to avoid adding ///<reference>
comments
我最终将site.d.ts重命名为global.d.ts(感谢),并在tsconfig中显式设置popper.js模块的路径:
I've ended up with renaming site.d.ts to global.d.ts (thanks to @itminus answer), and explicitly setting path to popper.js module in tsconfig:
{
"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" ]
}
popper.js是bootstrap 4的依赖项.这有点棘手,因为@types/bootstrap
依赖于popper.js
而不是@types/popper.js
(这是不兼容的,并且会在@ types/bootstrap/index.d中引发打字错误. .ts);
popper.js is dependency of bootstrap 4. It's kind of tricky, because @types/bootstrap
is dependent on popper.js
rather than @types/popper.js
(which is incompatible and would throw typescript errors in @types/bootstrap/index.d.ts);
幸运的是,popper.js包含打字稿定义(index.d.ts),我只需要告诉打字稿编译器在哪里查看它即可.
Fortunately, popper.js contains typescript definitions (index.d.ts), I just had to tell typescript compiler where to look at it.
推荐答案
我想原因是您将site.ts
和site.d.ts
放在了同一文件夹下.结果,仅加载了site.ts
文件.
I guess the reason is that you're having the site.ts
and site.d.ts
under the same folder. As a result, only the site.ts
file is loaded.
请参见在此处进行讨论
将site.d.ts
移至其他文件夹,或将其重命名为site.global.d.ts
.两者对我来说都完美无缺.
Move your site.d.ts
to other folders , or rename it to site.global.d.ts
. Both work flawlessly for me.
这篇关于如何使用libman代替npm下载打字稿定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!