【问题标题】:ES6 import hides TypeScript definition fileES6 导入隐藏 TypeScript 定义文件
【发布时间】:2017-02-02 14:36:13
【问题描述】:

我有 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 的那一刻,我的整个应用程序不再能够“看到”它的内容。

例如,将 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 { 

}

我错过了什么?

【问题讨论】:

    标签: typescript visual-studio-2015 typescript1.8 typescript-typings


    【解决方案1】:

    在文件中添加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 { 
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-17
      • 2018-11-17
      • 1970-01-01
      • 2016-03-11
      • 1970-01-01
      • 2016-06-24
      • 2015-08-13
      • 2017-03-11
      相关资源
      最近更新 更多