【问题标题】:Declaring an interface in .d.ts file with a type from third party library causes compile error使用第三方库中的类型在 .d.ts 文件中声明接口会导致编译错误
【发布时间】:2019-11-09 02:00:43
【问题描述】:

我的项目是在 Angular 7 上开发的,我创建了一个定义文件以使其在整个应用程序中都可用,而无需导入类型:

// globals.d.ts

interface ISomethingA {
  name: string;
  age: number;
}

但是当我尝试使用第三方库中的类型声明接口时出现问题:

// globals.d.ts

import { SomeTypeFromSomeWhere } from ‘some-library’;

interface ISomethingB {
  propA: SomeTypeFromSomeWhere
}

应用程序仍然可以运行,但会导致编译错误:Cannot find name 'ISomethingB’。是否不鼓励导入基于第三方库的类型?任何见解将不胜感激!

【问题讨论】:

    标签: angular typescript


    【解决方案1】:

    我这样用没问题:

    文件 a.model.ts:

        export interface SomeTypeFromSomeWhere {
          prop: string;
        }
    

    文件 b.model.ts:

        import { SomeTypeFromSomeWhere } from './a.model';
    
        interface ISomethingB {
           propA: SomeTypeFromSomeWhere;
        }
    

    希望这会有所帮助...

    【讨论】:

    • 我自己声明一个接口并通过导入在其他文件中使用它没有问题。我要问的是,当我尝试在 public 定义文件中声明一个接口时,该文件具有如上所述的第三方库类型的属性。
    猜你喜欢
    • 2020-07-02
    • 1970-01-01
    • 1970-01-01
    • 2015-10-14
    • 2011-06-18
    • 2019-03-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-13
    相关资源
    最近更新 更多