JuKe
JuKe

Reputation: 673

type definition for constructor functions and properties of a constructor function

Lets say we have a library that creates a constructor function for us. We can pass a Object as the first parameter and this properties will added to the constructor function directly.

this example can look like:

let Tiger = can.Construct.extend({
    leg: 3
});

We have now 2 possibilities:

  1. create an instance let bar = new Tiger('foo', 'bar')
  2. modify the constructors properties Tiger.leg = 4

second will throw an error with my current type definitios

Property 'leg' does not exit on type ConstructFactory

my current type definition looks like:

declare namespace can {

    interface ConstructFactory {
        new(...args: any[]): Construct;
    }

    class ConstructStatic {
        static extend(staticProperties: Object): ConstructFactory;
    }

    class Construct extends ConstructStatic {
        constructor(...args: any[]);
    }
}

if i return a type any instead of ConstructFactory the Tiger.leg will not throw an error, but now i can't create an instance because the constructor definition is lost.

Upvotes: 2

Views: 1305

Answers (1)

Corey Alix
Corey Alix

Reputation: 2750

I think this does what you're looking for:

declare namespace can {

    interface ConstructFactory {
        new(...args: any[]): Construct;
    }

    class ConstructStatic {
        static extend<T>(staticProperties: T): ConstructFactory & T;
    }

    class Construct extends ConstructStatic {
        constructor(...args: any[]);
    }
}


let Tiger = can.Construct.extend({
    leg: 3
});

Tiger.leg = 4

let bar = new Tiger('foo', 'bar')

Upvotes: 2

Related Questions