在我的部分代碼中,我收到一個無法解決的打字稿錯誤。作為測驗,我對一個值進行了本地分配,以查看如何修復它:
const t = ... // where t's type is: d3.Transition<SVGElement, ParseTreeHierarchyNode, SVGElement, IParseTreeRenderNode>
const tt: d3.Transition<Element, any, any, any> = t;
問題是第一個型別引數。SVGElement擴展Element,因此它們應該是分配兼容的。然而我得到這個錯誤:
Type 'Transition<SVGElement, ParseTreeHierarchyNode, SVGElement, IParseTreeRenderNode>' is not assignable to type 'Transition<Element, any, any, any>'.
The types returned by 'selection().datum(...).datum(...).on(...)' are incompatible between these types.
Type 'ValueFn<SVGElement, any, void>' is not assignable to type 'ValueFn<Element, any, void>'.
Type 'Element' is not assignable to type 'SVGElement'.ts(2322)
特別注意最后一行。錯誤狀態我會嘗試分配Element給SVGElement,這當然是不可能的。但事實上,這不是我正在做的。不知何故,打字稿似乎顛倒了分配和錯誤。
我能做些什么來解決這個問題?
uj5u.com熱心網友回復:
偶然我發現了導致問題的原因:因為帶有 D3.js 代碼的檔案本來是要在 vscode webview 中使用的,因此需要以不同的方式進行轉譯。因此,我將它從擴展的正常 TS 構建中排除。
一旦我包含它,問題就消失了。不知道為什么,但這為我修復了它,即使現在創建了一個錯誤的輸出檔案(這是另一個問題)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/471086.html
