angular - TypeScript Path2D 类缺少字符串构造函数

标签 angular typescript svg ionic2

我正在尝试从 SVG 字符串路径创建 Path2D 对象。根据Path 2D documentation来自 Mozilla,可以将字符串路径作为参数传递,但是,当我在代码中尝试它时,Webstorm IDE 向我显示此错误:

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

我发现声明 Path2D 的 lib.d.ts 没有 Path2D 类的字符串构造函数。

我该如何解决这个问题?我正在使用 Typescript 2.2.1

最佳答案

存在 Unresolved 错误 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);

另见

关于angular - TypeScript Path2D 类缺少字符串构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43773190/

相关文章:

angular - TypeScript 中的双重类型声明是什么意思?

使用 ngx-pica 图像缩放器时,Angular 5 卡住 UI

node.js - 扩展也是一个函数的 .d.ts 模块

typescript - 仅黑白差异排除和省略(选择和排除) typescript

javascript - SVG 路径的透视变换(四个 Angular 扭曲)

css - SVG stroke-dashoffset 不适用于 safari

d3.js - 使用 d3 v4 创建无限的水平轴

Angular 11,:enter and :leave animation not working with *ngIf

angularjs - angular2 中 angular.equals 的等价物

angular - 第一次加载页面时调用 ngOnDestroy