英文:
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代码中。我尝试了一些解决方法,但它们没有奏效:
- 使用
import type { DotNet } from "@microsoft/dotnet-js-interop"
会导致(TS) 'DotNet' 不能用作值,因为它是使用 'import type' 导入的
,这会在引用DotNet
的地方出现; - 当尝试像
///<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 "moduleResolution": "node"
is set in my tsconfig.json
. However, if I do import { DotNet } from "@microsoft/dotnet-js-interop"
, then this import is kept in transpiled JavaScript code. I tried a few workarounds, but they didn't work:
- Using
import type { DotNet } from "@microsoft/dotnet-js-interop"
results in(TS) 'DotNet' cannot be used as a value because it was imported using 'import type'.
whereverDotNet
is being referenced; - I cannot for my life get any results when trying things like
///<reference types/path="@microsoft/dotnet-js-interop or node_modules/@microsoft/dotnet-js-interop/dist/Microsoft.JSInterpop.d.ts">
or modifyingtsconfig.json
values such aspaths
(with"baseUrl"="."
) ortypeRoots
. All theses result in error(TS) Cannot find name 'DotNet'
.
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("@microsoft/dotnet-js-interop").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("@microsoft/dotnet-js-interop").DotNet;
Put this at the top of a file and you're good to go.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论