声明前使用的变量 'Foo'。两个类相互依存

Variable 'Foo' used before declaration. Two classes interdependence

请帮我解决问题。

tslint.json 中的

"no-use-before-declare"true。而且我不允许更改它。

问题如下 - "variable 'foo' used before declaration" 构建错误。

代码可以简化为:

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

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

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

这之前已作为 bug on tslint 提交,决议是 类 未提升且不能在声明前使用。在这种情况下规则是正确的。

你可以这样做:

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;

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