nodejs如何在typescript项目中申明全局变量

这样做的目的是避免循环引用,编写多余的类型文件

//global.d.ts
import type { A } from "./a";
import type { B } from "./b";
declare global {
  var a: A;
  var b: B;
}
//index.ts
import { A } from "./a";
import { B } from "./b";
global.a = new A();
global.b = new B();
a.a();
b.b();
//a.ts
export class A {
  a() {
    console.log('a');
    b.b();
  }
  a2() {
    console.log('a2');
  }
}
//b.ts
export class B {
  b() {
    console.log('b');
    a.a2();
  }
}

到此这篇关于nodejs在typescript项目中申明全局变量的文章就介绍到这了,更多相关nodejs在typescript项目中申明全局变量内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

来源链接:https://www.jb51.net/javascript/321549uq0.htm

© 版权声明
THE END
支持一下吧
点赞10 分享
评论 抢沙发
头像
请文明发言!
提交
头像

昵称

取消
昵称表情代码快捷回复

    暂无评论内容