当开发人员使用隐式类型表达式(而不是下面的显式代码)声明类字段时,我们希望希望通过命令行选项配置TSC来生成错误。

class Appliance {
    //coding style we want to enforce:
    private _group:Group = new Group();

    //coding style we want to prevent by issuing error:
    private _group = new Group();

    //coding style we want to enforce as it does not rely only on
    //constructor parameters list for declaration
    public assetTag:EquipmentTag;

    public constructor(assetTag:AssetTag,
        //coding style we want to prevent by issuing error if
        //no explicit declaration above constructor
        supplier:Company) {

        this.assetTag = assetTag; //coding style we want to enforce
    }

}

我们对TSC使用严格模式,但是它仍然允许使用一些随意的编码样式,如图所示。

我们是否可以使用任何TSC选项来超越严格模式并强制遵守某些编码风格?

最佳答案

正如Titian所建议的那样,代码样式由ts-lint https://palantir.github.io/tslint/强制执行

您正在寻找的规则:https://palantir.github.io/tslint/rules/typedef/

10-06 11:13