【问题标题】:Eslint flags as not defined class member that acts as default argument valueEslint 标记为未定义的类成员,充当默认参数值
【发布时间】:2021-02-01 16:26:02
【问题描述】:

为什么从以下位置升级后:

    "@typescript-eslint/eslint-plugin": "^4.13.0",
    "@typescript-eslint/parser": "^4.13.0",

到:

    "@typescript-eslint/eslint-plugin": "^4.14.0",
    "@typescript-eslint/parser": "^4.14.0",

eslint 已开始在下面的类中标记(用 Typescript 编写)currentPalette 未定义 [eslint(no-undef)],它充当whichPalette 的默认参数?

class Colormap {

    private currentPalette: string;

    getColors(numberColors: number, whichPalette = this.currentPalette): string[] {
        const colors = palette(whichPalette, numberColors);
        return colors.map((color) => `#${color}`);
    }
}

感谢您的澄清!

【问题讨论】:

  • 好吧,它没有值
  • 初始化currentPalette 并不能解决问题。对不起。

标签: typescript eslint


【解决方案1】:

无法使默认参数起作用。于是我又回到了老方法:

class Colormap {

    private currentPalette = "";

    getColors(numberColors: number, whichPalette?: string): string[] {
        const colors = palette(whichPalette ?? this.currentPalette, numberColors);
        return colors.map((color) => `#${color}`);
    }
}

这可以满足 eslint 的需求。 祝你今天过得愉快! 马里奥

【讨论】:

    猜你喜欢
    • 2019-10-05
    • 1970-01-01
    • 1970-01-01
    • 2015-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-06
    相关资源
    最近更新 更多