【问题标题】:Variable 'Foo' used before declaration. Two classes interdependence声明前使用的变量“Foo”。两个类相互依赖
【发布时间】:2019-04-02 15:43:56
【问题描述】:

请帮我解决问题。

tslint.json 中的"no-use-before-declare"true。而且我不允许更改它。

问题如下 - “声明前使用变量 'foo'”构建错误。

代码可以简化为:

export class One {
    toSecond() : Two {
        return new Two();
    }
}

export class Two {
    toFirst() : One {
        return new One();
    }
}

是否可以通过某种方式破解 linter 警告并获得相同的结果。有什么解决方法吗?

【问题讨论】:

    标签: angularjs typescript tslint


    【解决方案1】:

    你可以这样做:

    let Two_forward: typeofTwo;
    
    export class One {
        toSecond() : Two {
            return new Two_forward();
        }
    }
    
    export class Two {
        toFirst() : One {
            return new One();
        }
    }
    // Work around https://github.com/palantir/tslint/issues/3655
    type typeofTwo = typeof Two;
    Two_forward = Two;
    

    但 IMO 与仅使用 // tslint:disable-next-line:no-use-before-declare 抑制 lint 错误相比,这是不合理的。 (如果提议的strictLocalInitialization 选项here 成为strict 的一部分,则可能需要进一步更改。)

    【讨论】:

    • 非常感谢@Matt,TSLint 规则标志是目前对我来说最好的解决方案。
    【解决方案2】:

    这以前是作为bug on tslint 提交的,解决方法是类没有提升,并且在声明之前不能使用。在这种情况下规则是正确的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-04-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多