TypeScript声明使用静态方法扩展类。

时间:2015-04-12 19:50:36

标签: typescript

我正在寻找有关如何正确处理这种情况的一些建议。

我有something.d.ts

export class Sprite {

    static fromFrame(frameId: string): Sprite;
    static fromImage(imageId: string, crossorigin?: boolean, scaleMode?: number): Sprite;

}

export class TilingSprite extends Sprite {

    static fromFrame(frameId: string, width?: number, height?: number): TilingSprite;
    static fromImage(imageId: string, width?: number, height?: number, crossorigin?: boolean, scaleMode?: number): TilingSprite;

}

在这种情况下,我收到以下错误:

Error   40  Class static side 'typeof TilingSprite' incorrectly extends base class static side 'typeof Sprite'.   Types of property 'fromImage' are incompatible.
    Type '(imageId: string, width?: number, height?: number, crossorigin?: boolean, scaleMode?: number) => ...' is not assignable to type '(imageId: string, crossorigin?: boolean, scaleMode?: number)
=> Sprite'.
      Types of parameters 'width' and 'crossorigin' are incompatible.
        Type 'number' is not assignable to type 'boolean'.

我无法真正找到解决问题的方法,或者它与我期望的行为不同。

我怎么能干净地解决这个签名?

1 个答案:

答案 0 :(得分:4)

  

我怎样才能干净地解决这个签名

不是干净地但可以通过功能重载来完成。例如:

declare class Sprite {
    static fromImage(imageId: string, crossorigin?: boolean, scaleMode?: number): Sprite;
}

declare class TilingSprite extends Sprite {
    static fromImage(imageId: string, crossorigin?: boolean, scaleMode?: number): Sprite;
    static fromImage(imageId: string, width?: number, height?: number, crossorigin?: boolean, scaleMode?: number): TilingSprite;
}