【问题标题】:Typescript cannot extends Autodesk.Viewing.ToolInterface打字稿不能扩展 Autodesk.Viewing.ToolInterface
【发布时间】:2021-07-16 09:27:05
【问题描述】:

我正在按照 Autodesk 的教程创建新的查看器工具,但使用的是 Typescript。但是,当我尝试扩展 ToolInterface 时,我得到了:

TS2689 (TS) 无法扩展接口“Autodesk.Viewing.ToolInterface”。您指的是“实现”吗?

如果我使用工具,错误就会消失,但是我不能在构造函数中使用 super(),如教程中所述。我能得到一些帮助吗? 在forge-viewer的index.d.ts中,ToolInterface被定义为Interface,而不是Class

这是我正在学习的教程:

https://forge.autodesk.com/blog/custom-tools-forge-viewer

【问题讨论】:

  • 在他们的示例中称为工具接口的东西不是接口,所以我很困惑为什么打字稿会告诉你它是。您必须显示您正在使用的代码。
  • 我没有走多远,因为那是代码的第一行。这就是我所拥有的: const ToolName = 'TransformScaleTool'; const ToolOverlayName = 'TransformScaleOverlay';导出默认类 TransformScaleTool 实现 Autodesk.Viewing.ToolInterface { state = '';构造函数() { 超级(); this.state = ''; } }
  • 这个项目很大,所以我不能把所有的东西都发上来。但是当我为 ToolInterface 使用实现时,Visual Studio 指向 node_modules/forge-viewer 中的 index.d.ts。

标签: typescript autodesk-forge autodesk-viewer


【解决方案1】:

正如 cmets 中有人正确解释的那样,Autodesk.Viewing.ToolInterface 实际上是一个类。 TypeScript definition 错误地将其定义为导致问题的接口。

我们将需要更新 TypeScript defs,但与此同时,您可以尝试将自己的工具定义为一个简单的类,而不是扩展或实现任何东西,并且在使用 viewer.toolController.registerTool 向查看器注册该工具时,您可以将其强制键入您需要的任何类型,例如:

viewer.toolController.registerTool(myTool as Autodesk.Viewing.ToolInterface);

【讨论】:

    猜你喜欢
    • 2019-09-18
    • 2019-05-04
    • 1970-01-01
    • 2020-05-13
    • 2017-04-27
    • 2013-02-27
    • 2020-11-18
    相关资源
    最近更新 更多