這樣做的目的是避免迴圈引用,編寫多餘的型別檔案
//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();
}
}