named-string-values
How a set of named string values is declared and used.
The rule
Agents read this text as written when they write and review code.
Declare a set of named string values as a const object, and derive the union type from it, so the object is the single source of truth. Consumers reference the object's members, such as Action.Add, never raw string literals.
// common/constants/Action.ts
export const Action = {
Add: 'add',
Remove: 'remove',
} as const;
export type Action = (typeof Action)[keyof typeof Action];
A lookup map keyed by the union, Record<Action, string>, may share the object's file, because a change to one always changes the other. A constant that only uses the union, such as a default value or a subset of members, gets its own file.
Type the discriminant field of a union family with the object's member, and narrow with the member too, in === comparisons and switch cases:
export interface FileAddedEvent {
kind: typeof SyncEventKind.FileAdded;
path: string;
}
if (event.kind === SyncEventKind.FileAdded) { /* ... */ }
A raw literal stands only where there is no member to use:
- A union with no
constobject yet, such astype Mode = 'fast' | 'slow'. Declare the object first. - A file that cannot import the constant, because its package does not depend on the one declaring it or a build deliberately isolates its folder.
With a bare union, every call site types the raw string again, so the values are defined everywhere.
Examples
The check flags the incorrect code and passes the correct code. Each example is a small repo, because this rule looks across files. It opens on the file that matters; the other files are the repo around it.
Incorrect
// The literal leaks: every narrowing site retypes 'file-added'.export interface FileAddedEvent {kind: 'file-added';path: string;}export interface RecordParsedEvent {kind: 'record-parsed';recordId: string;}export type SyncEvent = FileAddedEvent | RecordParsedEvent;
Correct
import type { SyncEventKind } from '../constants/SyncEventKind';export interface FileAddedEvent {kind: typeof SyncEventKind.FileAdded;path: string;}export interface RecordParsedEvent {kind: typeof SyncEventKind.RecordParsed;recordId: string;}export type SyncEvent = FileAddedEvent | RecordParsedEvent;
Configure
- Block
"blocking"Stops a run when a file the run changed breaks the rule. - AdviseDefault
"advisory"Reports it and hands it to the refactor agent. Never stops a run. - Off
"off"Not checked. Use it when your own linter already enforces the rule.
Add this to your lightsout.config.json, then change the value.
{"standards-rule-settings": {"named-string-values": "advisory"}}