Multiple inheritance workarounds

Declaring a constructor on the same interface as the instance members doesn’t really make much sense — if you’re going to pass in a type dynamically to use in a constructor, it’s the static side of the class that would be restricted. What you would want to do is probably something like this:

interface Colorable {
    colorize(c: string): void;
}

interface Countable {
    count: number;
}

interface ColorCountable extends Colorable, Countable {
}

interface ColorCountableCreator {
    new(info: {color: string; count: number}): ColorCountable;
}

class ColorCounted implements ColorCountable {
    count: number;
    colorize(s: string) { }
    constructor(info: {color: string; count: number}) {
        // ...
    }
}

function makeThings(c: ColorCountableCreator) {
    var results: ColorCountable[];
    for(var i = 0; i < 10; i++) {
        results.push(new c({color: 'blue', count: i}));
    }
    return results;
}

var items = makeThings(ColorCounted);
console.log(items[0].count);

See also How does typescript interfaces with construct signatures work?

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)