【问题标题】:Store reference to namespace instead of type存储对命名空间而不是类型的引用
【发布时间】:2019-04-19 05:07:48
【问题描述】:

假设我有一个这样的命名空间:

export namespace Foo1 {
  export namespace Foo2 {
    export namespace Foo3 {
      export interface Foo4 {}
      export interface Foo5 {}
    }
  }
}

在 .ts 文件中我有这样的内容:

import {Foo1} from './foo';

const bar1 = function(){
   type t = Foo1.Foo2.Foo3.Foo4;
}

const bar2 = function(){
   type t = Foo1.Foo2.Foo3.Foo5;
}

这可能有点冗长,我希望做这样的事情:

import {Foo1} from './foo';

type Foo3 = Foo1.Foo2.Foo3;  // <<< this don't work, I get an error explained below

const bar1 = function(){
   type t = Foo3.Foo4;
}

const bar2 = function(){
   type t = Foo3.Foo5;
}

但我似乎无法存储对命名空间的引用,我只能存储对类型的引用? (我得到的错误是命名空间 Foo2 没有导出的成员 Foo3)。

【问题讨论】:

    标签: tsc typescript3.0


    【解决方案1】:

    我试过这样,效果很好

        namespace Shapes {
        export namespace Polygons {
            export namespace Square {
                export interface Foo4 {}
    
            }
    
            export namespace Triangle {
                export interface Foo5 {}
            }
        }
    }
    
    
    import polygons = Shapes.Polygons;
    
    type myType = polygons.Square.Foo4;
    
    export class myClass implements polygons.Square.Foo4{
        myFoo4 = 'Foo4';
    
    }
    
    
    const myFooInstance = new myClass();
    
    console.log(myFooInstance.myFoo4);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-04
      • 2011-12-31
      • 2015-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-04
      • 2018-07-15
      相关资源
      最近更新 更多