如何使用libman代替npm下载打字稿定义 [英] How to use libman instead of npm to download typescript definitions

查看:165
本文介绍了如何使用libman代替npm下载打字稿定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用npm安装打字稿定义时,可以在属于Visual Studio构建的打字稿文件中使用它们:

When I install typescript definitions using npm, I can use them in typescript files that are part of Visual Studio build:

  1. 在Visual Studio中创建新的ASP.NET Core Web应用程序
  2. 使用libman添加jQuery
  3. 使用npm install @types/jQuery
  4. 添加jQuery
  5. 创建新的打字稿文件:wwwroot/js/site.ts.这会将<TypeScriptCompile Include="wwwroot\js\site.ts" />添加到.csproj.
  6. 我现在可以在打字稿文件中使用$全局变量
  1. Create new ASP.NET Core Web Application in Visual Studio
  2. Add jQuery using libman
  3. Add jQuery using npm install @types/jQuery
  4. Create new typescript file: wwwroot/js/site.ts. This adds <TypeScriptCompile Include="wwwroot\js\site.ts" /> to .csproj.
  5. 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(感谢

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.tssite.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.

请参见在此处进行讨论

与.d.ts相比,解析过程始终偏爱.ts文件.这样做的合理性是可以从.ts文件生成.d.ts文件,因此.ts文件是真相的最新版本

The resolution process always favors the .ts files over .d.ts. the rational here is that a .d.ts file can be generated from the .ts file, so the .ts file is a more up-to-date version of the truth

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下载打字稿定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆