Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
538 views
in Technique[技术] by (71.8m points)

typescript - Eslint flags as not defined class member that acts as default argument value

Why after upgrading from:

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

to:

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

eslint has started to flag in the class below (written in Typescript) currentPalette as not defined [eslint(no-undef)] where it acts as default argument for whichPalette?

class Colormap {

    private currentPalette: string;

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

Thanks for clarifying!

question from:https://stackoverflow.com/questions/65847116/eslint-flags-as-not-defined-class-member-that-acts-as-default-argument-value

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

No way to make default argument work. So I returned to the good old way:

class Colormap {

    private currentPalette = "";

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

This works and satisfy eslint. Have a nice day! mario


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...