TS2345 错误,但是参数应该没问题吧?

问题描述

所以我有这个错误

public void RemoveNumber(int number) => _ = Numbers.Remove(number);

根据我的理解,TS2345: Argument of type '(bounds: any,fill: any,stroke: any,strokewidth: any) => mxShape' is not assignable to parameter of type 'new (...args: any) => mxShape'. Type '(bounds: any,strokewidth: any) => mxShape' provides no match for the signature 'new (...args: any): mxShape'. 应该足以满足给定的需求,但 Typescript 说,它不适合它。为什么?

我的 MxHeaderShape 是这样构建的:

new (...args: any): mxShape

解决方法

您似乎想要创建一个扩展 MxHeaderFooterShapemxShape 类。 假设您使用的是typed-mxgraph,您可以使用这种方式

import mx from './mxgraph'; // mxGraphExportObject returned by the mxgraph factory function
import { mxRectangle } from 'mxgraph';


class CustomMxShape extends mx.mxShape {

  constructor(bounds: mxRectangle,fill: string,stroke: string,strokewidth?: number) {
    super(bounds,fill,stroke,strokewidth);
  }

}

您还可以查看此演示:https://github.com/typed-mxgraph/typed-mxgraph-example-bundled-with-webpack/blob/7f0bf757aedb12cec84d86b4c4e4cbcb0f7a8147/src/app/custom-shapes.ts