Blazor JS interop with Typescript – proper DotNet declaration import

huangapple go评论67阅读模式
英文:

Blazor JS interop with Typescript - proper DotNet declaration import

问题

我正在编写一个Razor类库,其中包含从TypeScript转译而来的JavaScript互操作。TypeScript中的一些代码将引用全局可用的变量DotNet,该变量在初始化Blazor库时被包含。我能够在@microsoft/dotnet-js-interop Node包(作为devDependency添加)中找到DotNet模块声明,并且当在我的tsconfig.json中设置"moduleResolution": "node"时,TypeScript能够找到导出。然而,如果我这样做 import { DotNet } from "@microsoft/dotnet-js-interop", 那么这个导入会保留在转译后的JavaScript代码中。我尝试了一些解决方法,但它们没有奏效:

  1. 使用 import type { DotNet } from "@microsoft/dotnet-js-interop" 会导致 (TS) 'DotNet' 不能用作值,因为它是使用 'import type' 导入的,这会在引用 DotNet 的地方出现;
  2. 当尝试像 ///<reference types/path=" @microsoft/dotnet-js-interop 或 node_modules/@microsoft/dotnet-js-interop/dist/Microsoft.JSInterpop.d.ts"> 或修改 tsconfig.json 的值,如 paths(使用 "baseUrl"=".")或 typeRoots 时,我无论如何都得不到任何结果。所有这些都会导致错误 (TS) 找不到名称 'DotNet'

有没有办法正确导入DotNet模块声明,而不会在转译后的JavaScript代码中引入不必要的导入?

英文:

I'm writing a Razor Class Library that incorporates JavaScript interop transpiled from TypeScript. Some code in TypeScript will reference globally available variable DotNet which gets included when Blazor libraries are initialized. I was able to find DotNet module declaration in @microsoft/dotnet-js-interop Node package (added as devDependency) and TypeScript is able to find the export when &quot;moduleResolution&quot;: &quot;node&quot; is set in my tsconfig.json. However, if I do import { DotNet } from &quot;@microsoft/dotnet-js-interop&quot;, then this import is kept in transpiled JavaScript code. I tried a few workarounds, but they didn't work:

  1. Using import type { DotNet } from &quot;@microsoft/dotnet-js-interop&quot; results in (TS) &#39;DotNet&#39; cannot be used as a value because it was imported using &#39;import type&#39;. wherever DotNet is being referenced;
  2. I cannot for my life get any results when trying things like ///&lt;reference types/path=&quot;@microsoft/dotnet-js-interop or node_modules/@microsoft/dotnet-js-interop/dist/Microsoft.JSInterpop.d.ts&quot;&gt; or modifying tsconfig.json values such as paths (with &quot;baseUrl&quot;=&quot;.&quot;) or typeRoots. All theses result in error (TS) Cannot find name &#39;DotNet&#39;.

Is there any way to properly import DotNet module declaration without polluting transpiled JavaScript code with unnecessary imports?

答案1

得分: 1

代码部分不翻译,只翻译文本部分:

"It was tricky (lack of cohesive documentation didn't help), but I was able to figure it out myself."

这有点棘手(缺乏连贯的文档并没有帮助),但我能够自己解决它。

"The trick is to declare a variable and define its type with typeof import like so:

declare const DotNet: typeof import(&quot;@microsoft/dotnet-js-interop&quot;).DotNet;

Put this at the top of a file and you're good to go."

诀窍是声明一个变量并使用 typeof import 来定义其类型,就像这样:

declare const DotNet: typeof import("@microsoft/dotnet-js-interop").DotNet;

将其放在文件顶部,然后你就可以继续。

英文:

It was tricky (lack of cohesive documentation didn't help), but I was able to figure it out myself.

The trick is to declare a variable and define its type with typeof import like so:

declare const DotNet: typeof import(&quot;@microsoft/dotnet-js-interop&quot;).DotNet;

Put this at the top of a file and you're good to go.

huangapple
  • 本文由 发表于 2023年2月18日 19:36:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/75493058.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定