Enhancement: [no-shadow] option to ignore members of enums whose members are all literals
Before You File a Proposal Please Confirm You Have Done The Following...
- I have searched for related issues and found none that match my proposal.
- I have searched the current rule list and found no rules that match my proposal.
- I have read the FAQ and my problem is not listed.
- If I used AI assistance to prepare this issue, I have reviewed and followed the AI Contribution Policy.
My proposal is suitable for this project
- I believe my proposal would be useful to the broader TypeScript community (meaning it is not a niche proposal).
Link to the rule's documentation
https://typescript-eslint.io/rules/no-shadow
Description
#5385 asked for an option to ignore all enum members, and was declined because of the shadowing hazard: an initializer that reads the outer name gets the member instead.
This proposes a narrower option that keeps that case reported. Ignore a shadow by an enum member only when every member of that enum is a literal (string, number, or no initializer). A literal-only enum body never reads a name, so the shadow cannot change any value.
It's common for a string enum to have a member with the same name as a constant in scope, like a TOKEN header name next to a TOKEN value. Each clash needs an eslint-disable comment today, even though nothing in the enum can pick up the wrong value.
Suggested name: ignoreLiteralEnumMembers, default false.
Fail
/* eslint @typescript-eslint/no-shadow: ["error", { "ignoreLiteralEnumMembers": true }] */
const TOKEN = 'abc123';
enum Header {
TOKEN = 'x-token',
AUTH = TOKEN, // not a literal: reads Header.TOKEN, not the const
}Pass
/* eslint @typescript-eslint/no-shadow: ["error", { "ignoreLiteralEnumMembers": true }] */
const TOKEN = 'abc123';
enum Header {
TOKEN = 'x-token',
TRACE = 'x-trace',
}Additional Info
Josh-Cena made a similar point in #5385: most enums are literal-only, so the hazard is small for them. This limits the option to exactly those enums. Projects that enable prefer-literal-enum-member could turn this on everywhere. Happy to send the PR once the issue is accepted.
Source: typescript-eslint/typescript-eslint