Any way to declare a nest class structure in typescript?

I am interested in defining an existing structure (openlayers.d.ts), but I cannot figure out how to express the fact that OpenLayers.Layer is both a class and a namespace for OpenLayers.Layer.Markers. I believe this makes Markers a nested layer class.

using:

l = new OpenLayers.Layer(...); // this is a base class, never do this m = new OpenLayers.Layer.Markers(...); 

How would you declare the Layer and Markers class in typescript?

+43
typescript
Nov 21 '12 at 14:11
source share
6 answers

It seems to be fixed in versions 0.9.1.1 and later. You just need to create a module with the same name as the class in which you want to insert types, and paste your nested types into it.

More specifically, this is how you do it:

 declare module a { class b { } module b { class c { } } } var myB = new ab(); var myC = new abc(); 

This also works when nesting types in typescript code with the export keyword:

 export module a { export class b { } export module b { export enum c { C1 = 1, C2 = 2, C3 = 3, } } } 

As mentioned by @recursive in the comments below, the declaration order is important. Therefore, the class definition must be located in front of the module with nested types.

+50
Feb 20 '14 at 21:44
source share

You can use module merging to get an equivalent effect for nested classes. This example is adapted from Chapter 1 of Pro TypeScript.

You are merging modules with a class and module, or using a function and module.

In all cases, the module should appear after the class or merge function works.

Adapted example:

 class Outer { } module Outer { export class Mid { } export module Mid { export class Inner { } } } var a = new Outer(); var b = new Outer.Mid(); var x = new Outer.Mid.Inner(); 

Original answer

Note: this original answer (posted in 2012) applies to version 0.8 of TypeScript.

You can still create an ad that allows you to use the usage methods described by you, just the Markers object will be of type any . This can be improved if the work item of the nested class is fixed.

 declare module OpenLayers { export class Layer { constructor(param: string); static Markers(param: string); } } var l = new OpenLayers.Layer('...'); // this is a base class, never do this var m = new OpenLayers.Layer.Markers('...'); 
+12
Nov 21 '12 at 17:24
source share

As of 2016, I think it is easier:

 class A { static B = class { } } var a = new A(); var b = new AB(); 
+8
Feb 17 '16 at 2:17
source share

There is currently no support for nested classes.

http://typescript.codeplex.com/workitem/460

+7
Nov 21
source share

It's a bit late, but others may prove to be helpful. Here's how I did it to trick TS into completing a task.

 declare module OpenLayers { interface Layer { .... } interface Markers { .... } var Layer: { new(name:string, options?:any):Layer; prototype:Layer; Markers: { new(params?:any):Markers; prototype: Markers; } } } var markers = new OpenLayers.Layer.Markers(); 
+6
03 Jul. '13 at 23:12
source share

Use TypeScript expressions for this expression:

 var OpenLayers = class { static Layer = class { static Markers = class {} } } 
+3
Apr 14 '16 at 9:18
source share



All Articles