2017-05-04 62 views
2

我試圖從SVG字符串路徑創建Path2D對象。按照Path 2D documentation從Mozilla的,它可能通過一個字符串路徑作爲參數,但是,當我嘗試在我的代碼中,Webstorm IDE告訴我這個錯誤:TypeScript Path2D類缺少字符串構造函數

TS2345: Argument of type "" is not assignable to parameter of type 'Path2D' 

我想要的代碼執行是:

let p = new Path2D('M10 10 h 80 v 80 h -80 Z'); 

我發現該lib.d.ts,其中Path2D聲明,沒有爲Path2D類的字符串構造函數。

我該如何解決這個問題?我使用的是打字稿2.2.1

回答

2

有開放的bug Path2D missing string construtor

Wokraround是創建自己的類型

interface Path2DConstructor { 
    new(): Path2D; 
    new (d: string): Path2D; 
    new (path: Path2D): Path2D; 
    prototype: Path2D; 
} 
declare var Path2D: Path2DConstructor; 

var p = new Path2D('M10 10 h 80 v 80 h -80 Z'); 
ctx.fill(p); 

參見

+0

謝謝,@yurzui。我應該在哪裏放置這種和平的代碼?我試圖將它包含在lib.d.ts中,但不幸的是,我無法編輯它。 –

+0

您需要將此代碼插入文件的標題中,您將使用這些定義 – yurzui

+0

令人驚歎!它像一個魅力一樣工作!謝謝! –

相關問題