0

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

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)。

4

1 回答 1

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);

在此处输入图像描述

于 2018-11-16T04:01:22.253 回答