typescript - 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}`);
}
}
感谢您的澄清!
解决方案
无法使默认参数起作用。所以我回到了旧的好方法:
class Colormap {
private currentPalette = "";
getColors(numberColors: number, whichPalette?: string): string[] {
const colors = palette(whichPalette ?? this.currentPalette, numberColors);
return colors.map((color) => `#${color}`);
}
}
这有效并满足 eslint。祝你今天过得愉快!马里奥
推荐阅读
- macos - 在 OSX 上交叉编译 MIPS MT7688 OpenWRT (mipsel-unknown-linux-musl)
- pandas - 如何在选择结果中强制标题顺序?
- reactjs - react-native 将数据从第二个导航传递给父导航
- python-3.x - 如何并行迭代两个不同的列表,收敛到一个
- python - 通过python中的线程访问导入模块的locals()
- css - React-Native 文本不在两行上
- javascript - 即使在 Firefox 中更改,也会自动上传本地文件
- ios - 将图像保存到特定相册,但限制保存到相机胶卷文件夹
- mongodb - 使用会话的官方 mongo-go-driver
- imagemagick - Imagemagick FailedToExecuteCommand `"gswin32c.exe"