Waog
Waog

Reputation: 7245

Typescript: Implementing an Interface with a static/unnamed function

I need to implement the following (simplified) Typescript Interface of an d.ts file:

interface SomeLibrary {
    someProperty: SomePropertyClass;
    someMethod();
    (someParam: string): SomeClass; // don't know hot to implement this
}

How do I implement this interface in an own class? Especially the unnamed method is problematic. How are those methods called?

Upvotes: 2

Views: 631

Answers (1)

Fenton
Fenton

Reputation: 250872

The interface you have shown can't be satisfied by a class in TypeScript.

Interfaces like this are usually created to describe a library that uses a different pattern.

One example of how you can satisfy the interface is shown below:

var example: SomeLibrary = <any> function(someParam: string) {
    console.log('function called');
    return new SomeClass();
}
example.someProperty = new SomePropertyClass();
example.someMethod = function () { console.log('method called'); };

var x = example('x');
var y = example.someProperty;
var z = example.someMethod();

Upvotes: 2

Related Questions