生成声明时“无法命名”

时间:2016-05-14 23:32:27

标签: typescript

我在一个相当简单的情况下遇到了一个奇怪的错误。

档案export class ThingA { }

ThingB.ts

档案export class ThingB { }

Things.ts

档案export {ThingA} from "./ThingA"; export {ThingB} from "./ThingB"; (再出口)

Test.ts

file import * as things from "./Things"; export class Test { public method = (input: things.ThingA) => { }; } (使用重新导出的类)

tsc Test.ts --declaration --outDir compiled

这是我用来编译的:( typescript version 1.8.10)

Test.ts(5,5): error TS4029: Public property 'method' of exported class has or is using name 'ThingA' from external module ".../ThingA" but cannot be named.

这是我得到的错误:

Test.ts

问题在于为Test.ts生成声明文件。由于上述错误,所有文件都编译为.js,每个文件都生成.d.ts,但--declaration除外。如果从编译命令中删除things.ThingA,则没有错误。

一些观察结果:

1)things.ThingA的每个引用都不会出现错误。如果它作为方法参数引用,如提供的示例中所示,或作为返回值,则会显示错误。如果在方法体中引用它,则没有错误。我意识到这是因为在生成的.d.ts中甚至不会提到方法体中的引用,但我仍然不明白为什么tsc无法从Test.ts引用推断出实际的类类型。

2)如果我将import {ThingA,ThingB} from "./Things"; 中的import语句更改为:

import * as things

(而非ThingA)并进一步直接使用export,没有错误。但这并不是一个真正的解决方案,因为它会破坏目的或重新出口。

3)如果我只是从export class Test {中的Test.ts移除Test.d.ts,则没有错误,但Test.ts文件为空。即使不是,这也不是一个解决方案,因为这是实际代码的简化情况,其中Test的等价物在不同的文件中使用(导入),因此类public interface IRepository<T> where T : class { void Add(T entity); void Save(); } 必须出口。

我在这里做错了吗?

1 个答案:

答案 0 :(得分:3)

我怀疑这是一个bug,因此在typescript github上发布了一个问题,事实确实如此。

https://github.com/Microsoft/TypeScript/issues/8612

相关问题