Two members of an enum have the same value, or the enum mixes numbers and strings
What it finds
Two problems with the values of an enum's members:
- enum Status { Open = 1, Closed = 1 }: two members with the same literal value cannot be told apart at run time; Status.Closed === Status.Open holds, and a reverse lookup of a numeric enum finds only the last name. Usually a value was copied and not changed. - enum Mode { Off = 0, On = "on" }: an enum that mixes numbers and strings loses the reverse mapping for some members and makes checks such as typeof value === "number" unreliable.
Only members with a literal value are compared. Aliases, as Default = 0, None = 0, are sometimes meant, which is why the rule is recommended rather than on by default.
From the rule's source, enum_value_clash.rs.
enum Status { Open = 1, Closed = 1,tx0 reports: Closed has the same value as an earlier member Pending = 2,}enum Mode {tx0 reports: the enum Mode mixes number and string values Off = 0, On = "on",}enum Plain { A, B,} Turn it on, off or suppress it
The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:
[rules]
"tx0/ts:enum-value-clash" = "warning"To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:
// tx0: ignore[tx0/ts:enum-value-clash] reason="..."tx0 explain tx0/ts:enum-value-clash shows the same in the terminal. Found a false positive? Report it.