简体   繁体   English

如何使用libman而不是npm来下载typescript定义

[英]How to use libman instead of npm to download typescript definitions

When I install typescript definitions using npm, I can use them in typescript files that are part of Visual Studio build: 当我使用npm安装typescript定义时,我可以在作为Visual Studio构建的一部分的typescript文件中使用它们:

  1. Create new ASP.NET Core Web Application in Visual Studio 在Visual Studio中创建新的ASP.NET Core Web应用程序
  2. Add jQuery using libman 使用libman添加jQuery
  3. Add jQuery using npm install @types/jQuery 使用npm install @types/jQuery添加npm install @types/jQuery
  4. Create new typescript file: wwwroot/js/site.ts. 创建新的打样文件:wwwroot / js / site.ts。 This adds <TypeScriptCompile Include="wwwroot\\js\\site.ts" /> to .csproj. 这将<TypeScriptCompile Include="wwwroot\\js\\site.ts" />到.csproj。
  5. I can now use $ global variable in my typescript files 我现在可以在我的打字稿文件中使用$ global变量

However, when I use libman to download the typescript definitions, I have troubles configuring the typescript build to use them. 但是,当我使用libman下载打字稿定义时,我很难配置typescript构建来使用它们。

I guess I need to add tsconfig.json, but it seems to interfere with the TypeScriptCompile in the csproj. 我想我需要添加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

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? 如何修改项目(.csproj,tsconfig.json)以便我将使用wwwroot \\ lib \\ @types中的定义文件和全局变量而不是node_modules / @ types?

I've created tsconfig.json and set 我已经创建了tsconfig.json并设置了

  "compilerOptions": {
    "typeRoots": [ "wwwroot/lib/@types" ]
  }

but now the build returns errors like "Cannot find name MyInterface" in site.ts. 但现在构建在site.ts中返回错误,如“找不到名称MyInterface”。 The MyInterface is defined in site.d.ts. MyInterface在site.d.ts中定义。 Why the type was recognized when there was no tsconfig.json and now it doesn't? 为什么在没有tsconfig.json时识别出类型,现在却没有? I want to avoid adding ///<reference> comments 我想避免添加///<reference>注释

EDIT: 编辑:

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: 我最终将site.d.ts重命名为global.d.ts(感谢@itminus回答 ),并在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" ]
}

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是bootstrap 4的依赖。这有点棘手,因为@types/bootstrap依赖于popper.js而不是popper.js @types/popper.js (这是不兼容的,会在@ types / bootstrap / index中抛出@types/popper.js错误。 d.ts);

Fortunately, popper.js contains typescript definitions (index.d.ts), I just had to tell typescript compiler where to look at it. 幸运的是,popper.js包含typescript定义(index.d.ts),我只需告诉typescript编译器在哪里查看它。

I guess the reason is that you're having the site.ts and site.d.ts under the same folder. 我想原因是你在同一个文件夹下有site.tssite.d.ts As a result, only the site.ts file is loaded. 因此,仅加载site.ts文件。

See discussion here 这里的讨论

The resolution process always favors the .ts files over .d.ts. 解析过程总是优先于.d.ts上的.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 这里的理性是可以从.ts文件生成.d.ts文件,因此.ts文件是真实的更新版本

Move your site.d.ts to other folders , or rename it to site.global.d.ts . 将您的site.d.ts移动到其他文件夹,或将其重命名为site.global.d.ts Both work flawlessly for me. 两者都为我完美无瑕地工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 LibMan 下载目录? - How do I download directory with LibMan? 如何将我的Azure DevOps NPM Artifact提要用作libman.json中的提供者? - How do I use my Azure DevOps NPM Artifact feed as a provider in my libman.json? 如何在VS 2015中使用TypeScript定义 - How to use TypeScript definitions in VS 2015 如何在同一项目中为Gulp和RequireJS使用打字稿定义? - How can I use the typescript definitions for both Gulp and RequireJS in the same project? 如何在 Visual Studio React 项目中使用纱线代替 npm? - How to use yarn instead of npm in Visual Studio React project? Typescript的npm,NuGet和Marketplace下载选项之间有什么区别? - What are the differences among npm, NuGet and Marketplace download options for Typescript? 如何获取特定版本的Jquery,ex 3.2.1的TypeScript定义文件? - How to get TypeScript definitions file for a particular version of Jquery, ex 3.2.1? VS 2015 node.js,如何在打字稿中使用/调用NPM包? - VS 2015 node.js, how use/call NPM package in typescript? 如何将 TypeScript 定义添加到 .NET Core Nuget 包 - How to add TypeScript Definitions to .NET Core Nuget Packages TypeScript 类型定义 IntelliSense 错误 - TypeScript type definitions IntelliSense errors
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM