{"record":{"id":"9bc865e36d488f28","repo":"oxc-project/oxc","slug":"a-record-is-preferred-over-an-index-signature","errorCode":null,"errorMessage":"A record is preferred over an index signature.","messagePattern":"A record is preferred over an index signature\\.","errorType":"validation","errorClass":"OxcDiagnostic","httpStatus":null,"severity":"warning","filePath":"crates/oxc_linter/src/rules/typescript/consistent_indexed_object_style.rs","lineNumber":36,"sourceCode":"    rule::{DefaultRuleConfig, Rule},\n};\n\nfn consistent_indexed_object_style_diagnostic(\n    preferred: ConsistentIndexedObjectStyleConfig,\n    span: Span,\n) -> OxcDiagnostic {\n    let (warning_message, help_message) = match preferred {\n        ConsistentIndexedObjectStyleConfig::Record => (\n            \"A record is preferred over an index signature.\",\n            \"Use a record type such as `Record<string, unknown>` instead of an index signature.\",\n        ),\n        ConsistentIndexedObjectStyleConfig::IndexSignature => (\n            \"An index signature is preferred over a record.\",\n            \"Use an index signature such as `{ [key: string]: unknown }` instead of a record type.\",\n        ),\n    };\n\n    OxcDiagnostic::warn(warning_message).with_help(help_message).with_label(span)\n}\n\n#[derive(Debug, Clone, Default, Deserialize)]\npub struct ConsistentIndexedObjectStyle(ConsistentIndexedObjectStyleConfig);\n\n#[derive(Debug, Default, Clone, Copy, Eq, PartialEq, Deserialize, Serialize, JsonSchema)]\n#[serde(rename_all = \"kebab-case\")]\nenum ConsistentIndexedObjectStyleConfig {\n    /// When set to `record`, enforces the use of a `Record` for indexed object types, e.g. `Record<string, unknown>`.\n    #[default]\n    Record,\n    /// When set to `index-signature`, enforces the use of indexed signature types, e.g. `{ [key: string]: unknown }`.\n    IndexSignature,\n}\n\ndeclare_oxc_lint!(\n    /// ### What it does\n    ///","sourceCodeStart":18,"sourceCodeEnd":54,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/typescript/consistent_indexed_object_style.rs#L18-L54","documentation":"Warning from typescript/consistent-indexed-object-style (crates/oxc_linter/src/rules/typescript/consistent_indexed_object_style.rs:36). With the default option 'record', types keyed by strings/numbers must use Record<K, V> utility syntax; it fires on index signatures like '{ [key: string]: unknown }' and suggests a Record alternative in the help text.","triggerScenarios":"oxlint runs with the default (or explicit) 'indexedObjectStyle': 'record' and the file declares 'type Dict = { [key: string]: unknown };', a parameter typed '{ [k: number]: Foo }', or an interface with an index signature member.","commonSituations":"Enabling recommended presets over a codebase that predates Record; mixing styles after merging projects; interfaces that legitimately need both named members and an index signature (Record cannot express named members directly).","solutions":["Rewrite as 'Record<string, unknown>' / 'Record<number, Foo>'","When the type also has named members, split it: keep the interface for named members and add a Record-typed intersection or an index property typed as Record","If index signatures are the house style, set the option to 'index-signature' in .oxlintrc.json"],"exampleFix":"// before\ntype Dict = { [key: string]: unknown };\n\n// after\ntype Dict = Record<string, unknown>;","handlingStrategy":"validation","validationCode":"const INDEX_SIG = /\\[\\s*(key|k)?\\s*:\\s*(string|number|symbol)\\s*\\]\\s*:/;\nif (INDEX_SIG.test(source)) fail('index signature found; prefer Record<K, V>');","typeGuard":"function isRecordOf<V>(v: unknown): v is Record<string, V> {\n  return typeof v === 'object' && v !== null;\n}","tryCatchPattern":null,"preventionTips":["Default to Record<string, unknown> for dictionaries in style guides","Remember interfaces needing named members plus indexing stay as interfaces (rule cannot always fix)","Run the rule in CI on changed files to stop new index signatures"],"tags":["typescript","oxlint","lint","record-type","index-signature","style"],"backgroundTag":"record-vs-index-signature","analyzedSha":"e1e7af627c8843ab64044ed466b128fcc21a035b","analyzedAt":"2026-08-20T07:01:07.079Z","contentChangedAt":"2026-08-20T07:01:07.079Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}