Any way to declare a nest class structure in typescript?

前端 未结 6 1961
盖世英雄少女心
盖世英雄少女心 2020-12-05 06:35

I\'m interested in defining an existing framework (openlayers.d.ts) but cannot figure out how to express the fact that OpenLayers.Layer is both a class and a namespace for O

6条回答
  •  眼角桃花
    2020-12-05 06:59

    This seems like it has been fixed in versions 0.9.1.1 and later. You just have to create a module with the same name as the class where you want to nest types, and put your nested types in it.

    More concretely, this is how you do it:

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

    This works as well 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 the user @recursive in the comments below, the order of declaration is important. So the class definition must be located before the module with the nested types.

提交回复
热议问题