3

I'm trying to create a Path2D object from an SVG string path. According to the Path 2D documentation from Mozilla, it's possible to pass a string path as the parameter, however, when I try it in my code, the Webstorm IDE show me this error:

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

The code I'm trying to execute is:

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

I've found out that the lib.d.ts, where the Path2D is declared, does not have a string constructor for the Path2D class.

How can I solve this problem? I'm using Typescript 2.2.1

Athus Vieira
  • 403
  • 1
  • 4
  • 14

1 Answers1

2

There is open bug Path2D missing string construtor

Wokraround is creating your own types

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);

See also

Community
  • 1
  • 1
yurzui
  • 205,937
  • 32
  • 433
  • 399
  • thank you, @yurzui. Where should I place this peace of code? I'm trying to include it in the lib.d.ts but unfortunately, I can't edit it. – Athus Vieira May 04 '17 at 10:42
  • You need to insert this code into the header of the file where you will use these definitions – yurzui May 04 '17 at 11:06