首页 > 解决方案 > Eslint 标记为未定义的类成员,充当默认参数值

问题描述

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

    "@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}`);
    }
}

感谢您的澄清!

标签: typescripteslint

解决方案


无法使默认参数起作用。所以我回到了旧的好方法:

class Colormap {

    private currentPalette = "";

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

这有效并满足 eslint。祝你今天过得愉快!马里奥


推荐阅读