3

I am looking for some advice on how to handle this situation correctly.

I have 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;

}

In this case, I get the following error:

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'.

I cannot really see a way to solve the issue or, it is different behaviour from what I would expect.

How could I cleanly solve this signature?

1 Answer 1

4

How could I cleanly solve this signature

Not cleanly but can be done with function overloading. e.g.:

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;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks once again @basarat

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.