1

我有2個定義文件:foo.d.ts和bar.d.tsES6進口隱藏打字稿定義文件

// foo.d.ts 
interface IBaseInterface { 
    // stuff 
} 

// bar.d.ts 
interface IDerivedInterface extends IBaseInterface { 
    // more stuff 
} 

這工作得很好。當我將一個ES6導入添加到foo.d.ts時,我的整個應用程序不再能夠「看見」它的內容。

例如,修改foo.d.ts以下幾點:

// foo.d.ts 
import { SomeClass } from 'my-module'; 

interface IBaseInterface { 
    baz: SomeClass; 
} 

執行以下操作以bar.d.ts:

// bar.d.ts 
// ERROR: Cannot find name IBaseInterface 
interface IDerivedInterface extends IBaseInterface { 

} 

我缺少什麼?

回答

3

import添加到您的文件使其成爲一個模塊,這意味着當前文件中定義的內容對全局範圍內的內容不可見。

爲了解決這個問題,出口IBaseInterface,然後從已在定義IDerivedInterface文件導入。例如,你可以寫

// foo.d.ts 
import { SomeClass } from 'my-module'; 

export interface IBaseInterface { 
    baz: SomeClass; 
} 

// bar.d.ts 
import { IBaseInterface } from './foo'; 

interface IDerivedInterface extends IBaseInterface { 

}