2017-06-21 39 views
1

我如何引用typecript定义的嵌套属性的类型?这是我的尝试:Typescript:如何引用接口上的嵌套类型?

// def.d.ts 
declare module Def { 
    type TFoo = { 
     bar: (...args) => void; 
    } 
} 

// script.ts 
const bar: Def.TFoo.bar = function() {}; // Error TS2694: Namespace 'Def' has no exported member 'TFoo' 

我知道我可以单独定义并引用它:

// def.d.ts 
declare module Def { 
    type TFooBar = (...args) => void; 
    type TFoo = { 
     bar: TFooBar; 
    } 
} 

// script.ts 
const bar: Def.TFooBar = function (...args) {}; 

但我想用的定义更命名空间的方式,就像上面的例子。我如何实现这一目标?

回答

2

一个类型别名不是一个名字空间,你不能像那样引用它的内部属性。
只需使用另一个命名空间/模块:

declare module Def { 
    module TFoo { 
     type bar = (...args) => void; 
    } 
}