2

I would like to build a mixin and have it so it can be applied to different classes (in this case "Sprite" and "Graphics").

Line three does not work:

function Animated<T extends Constructor<Sprite> | Constructor<Graphics>>(Base: T) {

TypeScript complains that "T is not a constructor function".

import { Sprite, Texture, Container, Graphics } from "pixi.js";

type Constructor<T = {}> = new (...args: any[]) => T;

function Animated<T extends Constructor<Sprite> | Constructor<Graphics>>(Base: T) {
  return class extends Base {
    constructor(...args) {
      super(...args);
    }

    animate() {
      console.log('animte it');
    }
  }
}

export class AnimatedSprite extends Animated(Sprite) {
  constructor(texture? : Texture) {
    super(texture);
  }
}

export class AnimatedContainer extends Animated(Graphics) {
  constructor(nativeLines?: boolean) {
    super(nativeLines);
  }
}

1 Answer 1

0

Ok, found a solution:

export interface IAnimated {
    animate():void;
}

export const AnimatedSprite: Constructor<IAnimated> & typeof Sprite = <any>Animated(Sprite);
export const AnimatedGraphics: Constructor<IAnimated> & typeof Graphics = <any>Animated(Graphics);

Later (usage):

let s = new AnimatedSprite(someTexture); // has type "Sprite" and also interface IAnimated
let g = new AnimatedGraphics(false); // has type "Graphics" and also interface IAnimated
Sign up to request clarification or add additional context in comments.

Comments

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.