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?

Florent
  • 12,310
  • 10
  • 49
  • 58
chrischu
  • 3,047
  • 3
  • 27
  • 44
  • Possible duplicate of [Any way to declare a nest class structure in typescript?](http://stackoverflow.com/questions/13495107/any-way-to-declare-a-nest-class-structure-in-typescript) – Alex Oct 22 '15 at 08:13

2 Answers2

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.

iano
  • 2,061
  • 1
  • 18
  • 22
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;
Fenton
  • 241,084
  • 71
  • 387
  • 401
  • 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 :(. – chrischu Oct 09 '12 at 13:49
  • I've added a third attempt to see if that's what you are after. – Fenton Oct 09 '12 at 13:58
  • 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. – chrischu Oct 09 '12 at 14:11
  • Ah - okay - so you just want to be able to define an existing JavaScript file. – Fenton Oct 09 '12 at 14:12
  • In fact - you stated that in your question, so I apologise for missing that! – Fenton Oct 09 '12 at 14:48
  • Mhhh that would work somewhat but the thing is that with this solution I can't define the interface of the Request class, can I? – chrischu Oct 09 '12 at 16:59
  • You could define it and then use a cast... `var y = new x.Request();` – Fenton Oct 10 '12 at 08:10
  • Well the whole point of TypeScript definition files is to enable me to use OpenLayers as normal just with the added "type safety" at development time. I can't change the way OpenLayers builds and instantiates its classes. – chrischu Oct 10 '12 at 08:31