ES6 导入隐藏 TypeScript 定义文件

ES6 import hides TypeScript definition file

我有 2 个定义文件:foo.d.ts 和 bar.d.ts

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

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

这很好用。当我将 ES6 导入添加到 foo.d.ts 时,我的整个应用程序不再能够 "see" 它的内容。

例如修改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 { 

}

我错过了什么?

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 { 

}