4

I am currently trying to build a TypeScript definition file for OpenLayers.

The problem is that in OpenLayers there are certain classes that would translate to both a module and a class in TypeScript.

For example there is the Protocol class in module OpenLayers and there is a class Response in module OpenLayers.Protocol.

How could I model that in TypeScript? Can I make Protocol a class and define the Response class as a inner class that is exported? How would you go about solving that problem?

1

2 Answers 2

1

Declare Response as a static field of Protocol with a constructor type, returning an interface that defines the Response class:

declare module OpenLayers {
    export interface IProtocolResponse {
        foo(): void;
    }

    export class Protocol {
        static Response: new () => IProtocolResponse;
    }
}

var response = new OpenLayers.Protocol.Response();
response.foo();

Edit:

Or as Anders points out in this discussion list question, you can have multiple constructors for the inner class in this way:

declare module OpenLayers {
    export interface IProtocolResponse {
        foo(): void;
    }

    export class Protocol {
        static Response: {
            new (): IProtocolResponse;
            new (string): IProtocolResponse;
        };
    }
}

var response = new OpenLayers.Protocol.Response('bar');
response.foo();

The main downside of both approaches is that you cannot derive a class from OpenLayers.Protocol.Response.

Sign up to request clarification or add additional context in comments.

Comments

0

Here is my updated answer, which I hope helps - it should get you started on defining OpenType:

declare module OpenType {
   export class Protocol {
       constructor();
       Request;
   }
}

var x = new OpenType.Protocol();
var y = new  x.Request();
var z = x.Request;

8 Comments

Both solutions are not possible sadly. The first one because it isn't really what I want since Protocol.Response should hold the class not an instance of it. The second one is sadly also not possible since I can't just rename classes in OpenLayers :(.
I've added a third attempt to see if that's what you are after.
Ok the thing is that I am building TypeScript (typed JavaScript from Microsoft) definition files. They are not really an implementation but rather a kind of interface declaration. Therefore sadly also the third solution doesn't work.
Ah - okay - so you just want to be able to define an existing JavaScript file.
In fact - you stated that in your question, so I apologise for missing that!
|

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.