TS----Circular definition of import alias 'hello'.

625 阅读1分钟

简介

Circular definition of import alias 'hello'

导入的别名'APIClass'循环定义。

这种情况发生在两个或以上的模块相互引用同一变量名。

图示

image.png

demo

在同一目录下建以下三个文件。开启tslint。

// a.d.ts
import { hello } from '.';

export declare const v = hello;
// a.js
function hello() {
  console.log(hello);
}
export { hello };
// index.js
export { hello } from './a';

在a.d.ts文件中如下所示将会报错:

image.png

总结

上面的文件就是a.d.ts导入了index.js中的变量helloindex.js导入了a.d.ts中的hello。所以导致Circular definition of import alias 'hello'

这里有一点细节:关于编译时和运行时谁是模块。

index.js文件中是从'./a'中导入的hello,这里的a模块指的是a.d.ts。如果同一目录下不存在a.d.ts,编译时把a.js当做导入模块,如果存在a.d.ts,则把a.d.ts当做导入模块。运行时只能把a.js当做导入模块。