Any way to declare a nest class structure in typescript?

前端 未结 6 1974
盖世英雄少女心
盖世英雄少女心 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:54

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

    You perform merging with a class and a namespace, or with a function and a namespace.

    In all cases, the namespace must appear after the class or function for the merge to work.

    Adapted example:

    class Outer {
    
    }
    
    namespace 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();
    

提交回复
热议问题