duplicate-code-value
Two names for one code: two members of a code table with the same value, a literal union that names a member twice, or two case labels of one switch with the same value.
Why: codes numbered by hand (STAFF: 13, DOUSE: 14) collide when two branches each add one. Both pick the next free number, and the merge keeps both lines: git, when the lines don't touch, or whoever resolves the conflict by taking both sides. Nothing fails until a saved or sent 16 is read back as the wrong code. An adopter's game got job 16 and 17, world encodings 37 to 39 and the opcodes 0x3C / 0x3D twice this way. Guide: Enums.
✕ Rejectedduplicate-code-value
ts
export const Job = {
NONE: 0,
MOVE: 1,
GATHER: 2,
SCOUT: 3,
SCRUB: 3,
} as const;
export type JobCode = (typeof Job)[keyof typeof Job];✅ Accepted: codes derived from an append-only list of names, so a new code is an appended name
✓ Accepted0 diagnostics
ts
import { trust } from "@evobug_com/safe-ts-std/result";
type CodesOf<T extends readonly string[]> = {
readonly [I in keyof T as I extends `${number}` ? T[I] & string : never]: I extends `${infer N extends number}` ? N : never;
};
/** `{ NONE: 0, MOVE: 1 }`: each name's code is its index. */
export function codesOf<const T extends readonly string[]>(names: T): CodesOf<T> {
// `Object.fromEntries` can't say which keys it makes: each name, its index.
return trust<CodesOf<T>>(Object.freeze(Object.fromEntries(names.map((name, i) => [name, i]))));
}
export const Job = Object.freeze({ ...codesOf(["NONE", "MOVE", "GATHER", "SCOUT", "SCRUB"]) });
export type JobCode = (typeof Job)[keyof typeof Job];Two branches that each append a name still meet in a merge, but keeping both sides is now right: each name gets its own index. The checker reads the codes from codesOf's declared return type, as the JSON guide explains, and still compares them: a table built from two lists ({ ...codesOf(a), ...codesOf(b) }) starts both at 0, and that is reported too.
A switch over codes has the same problem from the other side:
✕ Rejectedduplicate-code-value
ts
export const Op = { Hello: 1, Bye: 2, Ping: 3 } as const;
export function describe(op: number): string {
switch (op) {
case Op.Hello:
return "hello";
case 1:
return "one";
default:
return "other";
}
}What counts
- A code table is a module-level
constwhose value TypeScript gives literal types: anas constobject,Object.freeze({ ... }), or an object of spreads ({ ...codesOf([...]), ...LEGACY }), here or imported. Its values must all be numbers or all strings. It counts when the file takes the union of its values ((typeof Job)[keyof typeof Job], orValueOf<typeof Job>), when it is marked/** @unique */, or when its values are whole numbers under capitalised names, close together (NONE: 0, MOVE: 1, GATHER: 2) or flags from 1 (1, 2, 4). - Not a code table: limits and sizes (
{ title: 80, body: 80 },{ Medium: 16, Large: 16 }), labels without a union type, objects of objects. Two equal values there are normal. - A deliberate alias says so in a comment on either member:
/** Alias of WALK. */,// alias of WORK, or@deprecatedon an old name kept for compatibility. In a table marked@unique, aliases are reported too. - A table of words for members, where the shared value is one of the table's own names (
{ help: "help", "?": "help" }, the parsing tablestring-dispatchasks for): the other names spelling it are aliases of that name. - A union of literal types naming one twice:
type Code = 1 | 2 | 2. TypeScript keeps one, so the member meant to be new is an old one. caselabels that are literals or constants (Op.Hello, an importedconst), compared by value:case 1:aftercase Op.Hello:is the same1. The second never runs. Labels written the same way (case "a":twice,case (Op.Hello):aftercase Op.Hello:, alsocase 0x1:aftercase 1:) are left to oxlint'sno-duplicate-case, which the preset turns on: one finding per label. This rule reports the ones that only turn out equal once evaluated (case Op["Hello"]:,case `a`:aftercase "a":, a constant imported from another file).
✅ Accepted: a deliberate alias, and a table that isn't codes
✓ Accepted0 diagnostics
ts
export const Phase = {
IDLE: 0,
WALK: 1,
/** @deprecated Use WALK. */
MOVE: 1,
} as const;
export type PhaseCode = (typeof Phase)[keyof typeof Phase];
export const Limits = { title: 80, body: 80 } as const;What the rule can't see
A table whose values are computed at run time ({ A: next() }) has no literal types to compare. A table typed by an annotation (const Job: Record<string, number> = { ... }) has none either. A code table in a form the heuristics don't count (lower-case names, wide gaps) counts once the file takes the union of its values or marks it @unique.