Angular 无法导入导出的接口-找不到导出

Angular 无法导入导出的接口-找不到导出,angular,typescript,ecmascript-6,Angular,Typescript,Ecmascript 6,我将问题简化为以下示例: 测试.model.ts export class A { a: number; } export interface B { b: number; } import { Component, Input } from '@angular/core'; import { A, B } from './test.model'; @Component({ selector: 'test', template: '<h1>tes

我将问题简化为以下示例:

测试.model.ts

export class A {
    a: number;
}

export interface B {
    b: number;
}
import { Component, Input } from '@angular/core';
import { A, B } from './test.model';

@Component({
    selector: 'test',
    template: '<h1>test</h1>'
})
export class TestComponent {

    //This works fine
    @Input() foo: A;

    //Does NOT work - export 'B' was not found in './test.model'
    @Input() bar: B;

}
测试组件.ts

export class A {
    a: number;
}

export interface B {
    b: number;
}
import { Component, Input } from '@angular/core';
import { A, B } from './test.model';

@Component({
    selector: 'test',
    template: '<h1>test</h1>'
})
export class TestComponent {

    //This works fine
    @Input() foo: A;

    //Does NOT work - export 'B' was not found in './test.model'
    @Input() bar: B;

}
但是,使用类也可以。有什么提示吗


更新:似乎与此问题有某种关联:

昨天我在这里发现了另一个问题,解决方案是将导出的接口声明为一个单独的文件。每个文件有一个接口,它对我来说没有任何警告

这对我很有用:

@Input() bar: B | null;

使用Angular 4.4.3+Typescript 2.3.4,我在自己的
myinterface.ts
文件中定义了一个接口,然后运行Karma测试,出现了这个错误。运行
ng service
时,我看不到问题。在
myinterface.ts
文件中添加1个未使用常量的
export
,解决了以下问题:

export interface IMyInterface {
    ...
}
export const A = 'A';

您还可以通过使用
Typescript 3.8
中的一个新功能来解决这个问题,该功能名为

这意味着您可以像这样导入有问题的类型
A

import { B } from './test.model';
import type { A } from './test.model';
还要注意,Angular似乎只支持来自的Typescript 3.8


我还建议你阅读。

根本原因在于打字脚本和透明。一旦传输了TypeScript代码,接口/类型就消失了。它们不再存在于发出的文件中

当类型被擦除时,导入/导出不一定被删除。原因是存在歧义,而且编译器并不总是确定导出的内容是类型还是值

使用TypeScript
3.8
及更高版本时,在
test.component.ts
中只需执行以下操作:

import { A } from './test.model';
import type { B } from './test.model';

这不是已经回答了吗?我不这么认为——这个问题涉及我不使用的默认导出。无论如何,谢谢。啊,对不起,没有正确读取。这是运行时或编译错误吗?这是一个编译警告。这对我来说很有效,下面是原因的解释:即使它是文件中唯一的接口,我也会收到此错误(但还有其他导出,例如
mergeInterfaces(左,右)
方法)。这似乎是一个诚实的bug,而不是一个特性。如果允许
null
作为有效的输入值,这将起作用(否则将违反类型安全)。