TypeScript Decorators and Circular Dependencies

后端 未结 3 443
小蘑菇
小蘑菇 2021-01-06 19:03

Consider the sample of inter-dependent code (below) that makes use of decorators.

Now consider the following workflow (yes, I do want to pass the actual exported cla

3条回答
  •  一向
    一向 (楼主)
    2021-01-06 19:31

    How about doing the same, but structuring your code differently?
    If both Child and Parent reside in the same file then it shouldn't be a problem.

    That might not sound optimal as it's comfortable to separate code to modules due to length and logic, but that can be solved in a way.
    You can have a main file that has base classes for those, even abstract:

    // Base.ts
    import {Test} from "./Decorators";
    
    @Test(BaseChild)
    export abstract class BaseParent {}
    
    @Test(BaseParent)
    export abstract class BaseChild {}
    

    And then in your specific modules:

    // Parent.ts
    import {BaseParent} from "./Base";
    
    export class Parent extends BaseParent {}
    

    And

    // Child.ts
    import {BaseChild} from "./Base";
    
    export class Child extends BaseChild {}
    

提交回复
热议问题