1"use strict";(self.webpackChunkwebsite=self.webpackChunkwebsite||[]).push([["163"],{4966(e,n,t){t.r(n),t.d(n,{metadata:()=>r,default:()=>h,frontMatter:()=>a,contentTitle:()=>c,toc:()=>d,assets:()=>u});var r=JSON.parse('{"id":"consistent-indexed-object-style","title":"consistent-indexed-object-style","description":"Require or disallow the `Record` type.","source":"@site/../eslint-plugin/docs/rules/consistent-indexed-object-style.mdx","sourceDirName":".","slug":"/consistent-indexed-object-style","permalink":"/rules/consistent-indexed-object-style","draft":false,"unlisted":false,"editUrl":"https://github.com/typescript-eslint/typescript-eslint/edit/main/packages/website/../eslint-plugin/docs/rules/consistent-indexed-object-style.mdx","tags":[],"version":"current","frontMatter":{"description":"Require or disallow the `Record` type.","image":"/img/og/rules/consistent-indexed-object-style.png"},"sidebar":"rulesSidebar","previous":{"title":"consistent-generic-constructors","permalink":"/rules/consistent-generic-constructors"},"next":{"title":"consistent-return","permalink":"/rules/consistent-return"}}'),s=t(65723),i=t(7143),o=t(70503),l=t(46742);let a={description:"Require or disallow the `Record` type.",image:"/img/og/rules/consistent-indexed-object-style.png"},c,u={},d=[{value:"Options",id:"options",level:2},{value:"<code>'record'</code>",id:"record",level:3},{value:"<code>'index-signature'</code>",id:"index-signature",level:3},{value:"When Not To Use It",id:"when-not-to-use-it",level:2},{value:"Resources",id:"resources",level:2}];function p(e){let n={a:"a",blockquote:"blockquote",code:"code",em:"em",h2:"h2",h3:"h3",li:"li",meta:"meta",p:"p",pre:"pre",ul:"ul",...(0,i.R)(),...e.components},{Head:t,RuleAttributes:r,TryInPlayground:a}=n;return t||x("Head",!0),r||x("RuleAttributes",!0),a||x("TryInPlayground",!0),(0,s.jsxs)(s.Fragment,{children:[(0,s.jsx)(s.Fragment,{children:(0,s.jsxs)(n.blockquote,{children:["\n",(0,s.jsxs)(n.p,{children:["Require or disallow the ",(0,s.jsx)(n.code,{children:"Record"})," type."]}),"\n"]})}),"\n",(0,s.jsx)(r,{name:"consistent-indexed-object-style"}),"\n","\n",(0,s.jsxs)(n.p,{children:["TypeScript supports defining arbitrary object keys using an index signature or mapped type.\nTypeScript also has a builtin type named ",(0,s.jsx)(n.code,{children:"Record"})," to create an empty object defining only an index signature.\nFor example, the following types are equal:"]}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-ts",children:"interface IndexSignatureInterface {\n [key: string]: unknown;\n}\n\ntype IndexSignatureType = {\n [key: string]: unknown;\n};\n\ntype MappedType = {\n [key in string]: unknown;\n};\n\ntype RecordType = Record<string, unknown>;\n"})}),"\n",(0,s.jsx)(n.p,{children:"Using one declaration form consistently improves code readability."}),"\n",(0,s.jsxs)(o.A,{children:[(0,s.jsx)(l.A,{value:"Flat Config",children:(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-js",metastring:'title="eslint.config.mjs"',children:'export default defineConfig({\n rules: {\n "@typescript-eslint/consistent-indexed-object-style": "error"\n }\n});\n'})})}),(0,s.jsx)(l.A,{value:"Legacy Config",children:(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-js",metastring:'title=".eslintrc.cjs"',children:'module.exports = {\n "rules": {\n "@typescript-eslint/consistent-indexed-object-style": "error"\n }\n};\n'})})})]}),"\n",(0,s.jsx)(a,{eslintrcHash:"N4KABGBEBOCuA2BTAzpAXGUEKQAIBcBPABxQGNoBLY-AWhXkoDt8B6MgeyeUuX0Ra1mAE0QAPRMNocARgCtEZOn0JJ0URNGgdokcGAC+IA0A",children:(0,s.jsx)(n.p,{children:"Try this rule in the playground \u2197"})}),"\n",(0,s.jsx)(n.h2,{id:"options",children:"Options"}),"\n",(0,s.jsx)(s.Fragment,{children:(0,s.jsx)(n.p,{children:"This rule accepts the following options:"})}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-ts",children:"type Options = [\n /** Which indexed object syntax to prefer. */\n | 'index-signature'\n /** Which indexed object syntax to prefer. */\n | 'record',\n];\n\nconst defaultOptions: Options = ['record'];\n"})}),"\n",(0,s.jsxs)(n.ul,{children:["\n",(0,s.jsxs)(n.li,{children:[(0,s.jsx)(n.code,{children:"'record'"})," ",(0,s.jsx)(n.em,{children:"(default)"}),": only allow the ",(0,s.jsx)(n.code,{children:"Record"})," type."]}),"\n",(0,s.jsxs)(n.li,{children:[(0,s.jsx)(n.code,{children:"'index-signature'"}),": only allow index signatures."]}),"\n"]}),"\n",(0,s.jsx)(n.h3,{id:"record",children:(0,s.jsx)(n.code,{children:"'record'"})}),"\n","\n",(0,s.jsxs)(o.A,{children:[(0,s.jsx)(l.A,{value:"\u274C Incorrect",children:(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-ts",metastring:'option=\'"record"\' eslintrcHash="N4KABGBEBOCuA2BTAzpAXGUEKQAIBcBPABxQGNoBLY-AWhXkoDt8B6MgeyeUuX0Ra1mAE0QAPRMNocARgCtEZOn0JJ0YANrhsORNGgdokADTadMRYeGQzYALraAviEdA"',children:"interface IndexSignatureInterface {\n [key: string]: unknown;\n}
1\n\ntype IndexSignatureType = {\n [key: string]: unknown;\n};\n\ntype MappedType = {\n [key in string]: unknown;\n};\n"})})}),(0,s.jsx)(l.A,{value:"\u2705 Correct",children:(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-ts",metastring:'option=\'"record"\' eslintrcHash="N4KABGBEBOCuA2BTAzpAXGUEKQAIBcBPABxQGNoBLY-AWhXkoDt8B6MgeyeUuX0Ra1mAE0QAPRMNocARgCtEZOn0JJ0YANrhsORNGgdokADTadMRYeGQzYALraAviEdA"',children:"type RecordType = Record<string, unknown>;\n"})})})]}),"\n",(0,s.jsx)(n.h3,{id:"index-signature",children:(0,s.jsx)(n.code,{children:"'index-signature'"})}),"\n",(0,s.jsxs)(o.A,{children:[(0,s.jsx)(l.A,{value:"\u274C Incorrect",children:(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-ts",metastring:'option=\'"index-signature"\' eslintrcHash="N4KABGBEBOCuA2BTAzpAXGUEKQAIBcBPABxQGNoBLY-AWhXkoDt8B6MgeyeUuX0Ra1mAE0QAPRMNocARgCtEZOn0JJ0YANrhsORNGgdokADTadkEeNo8A5kwCG+WNESQzYALraAviG9A"',children:"type RecordType = Record<string, unknown>;\n"})})}),(0,s.jsx)(l.A,{value:"\u2705 Correct",children:(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-ts",metastring:'option=\'"index-signature"\' eslintrcHash="N4KABGBEBOCuA2BTAzpAXGUEKQAIBcBPABxQGNoBLY-AWhXkoDt8B6MgeyeUuX0Ra1mAE0QAPRMNocARgCtEZOn0JJ0YANrhsORNGgdokADTadkEeNo8A5kwCG+WNESQzYALraAviG9A"',children:"interface IndexSignatureInterface {\n [key: string]: unknown;\n}\n\ntype IndexSignatureType = {\n [key: string]: unknown;\n};\n\ntype MappedType = {\n [key in string]: unknown;\n};\n"})})})]}),"\n",(0,s.jsx)(n.h2,{id:"when-not-to-use-it",children:"When Not To Use It"}),"\n",(0,s.jsx)(n.p,{children:"This rule is purely a stylistic rule for maintaining consistency in your project.\nYou can turn it off if you don't want to keep a consistent style for indexed object types."}),"\n",(0,s.jsx)(n.p,{children:"However, keep in mind that inconsistent style can harm readability in a project.\nWe recommend picking a single option for this rule that works best for your project."}),"\n",(0,s.jsxs)(s.Fragment,{children:[(0,s.jsx)(n.h2,{id:"resources",children:"Resources"}),"\n",(0,s.jsxs)(n.ul,{children:["\n",(0,s.jsx)(n.li,{children:(0,s.jsx)(n.a,{href:"https://github.com/typescript-eslint/typescript-eslint/blob/main/packages/eslint-plugin/src/rules/consistent-indexed-object-style.ts",children:"Rule source"})}),"\n",(0,s.jsx)(n.li,{children:(0,s.jsx)(n.a,{href:"https://github.com/typescript-eslint/typescript-eslint/blob/main/packages/eslint-plugin/tests/rules/consistent-indexed-object-style.test.ts",children:"Test source"})}),"\n"]})]}),"\n",(0,s.jsx)(t,{children:(0,s.jsx)(n.meta,{content:"consistent-indexed-object-style: Require or disallow the Record type",name:"twitter:image:alt"})})]})}function h(e={}){let{wrapper:n}={...(0,i.R)(),...e.components};return n?(0,s.jsx)(n,{...e,children:(0,s.jsx)(p,{...e})}):p(e)}function x(e,n){throw Error("Expected "+(n?"component":"object")+" `"+e+"` to be defined: you likely forgot to import, pass, or provide it.")}},46742(e,n,t){t.d(n,{A:()=>l});var r=t(65723);t(22155);var s=t(70851),i=t(28226);function o({children:e,className:n,hidden:t}){return(0,r.jsx)("div",{role:"tabpanel",className:(0,s.A)("tabItem_eQ9s",n),hidden:t,children:e})}function l({children:e,className:n,value:t}){let{selectedValue:s,lazy:a}=(0,i.uc)(),c=t===s;return!c&&a?null:(0,r.jsx)(o,{className:n,hidden:!c,children:e})}},70503(e,n,t){t.d(n,{A:()=>p});var r=t(65723);t(22155);var s=t(70851),i=t(98838),o=t(28226),l=t(26309),a=t(29021);function c({className:e}){let{selectedValue:n,selectValue:t,tabValues:i,block:a}=(0,o.uc)(),u=[],{blockElementScrollPositionUntilNextRender:d}=(0,l.a_)(),p=e=>{let r=e.currentTarget,s=i[u.indexOf(r)].value;s!==n&&(d(r),t(s))},h=e=>{let n=null;switch(e.key){case"Enter":p(e);break;case"ArrowRight":{let t=u.indexOf(e.currentTarget)+1;n=u[t]??u[0];break}case"ArrowLeft":{let t=u.indexOf(e.currentTarget)-1;n=u[t]??u[u.length-1]}}n?.focus()};return(0,r.jsx)("ul",{role:"tablist","aria-orientation":"horizontal",className:(0,s.A)("tabs",{"tabs--block":a},e),children:i.map(({value:e,label:t,attributes:i})=>(0,r.jsx)("li",{role:"tab",tabIndex:n===e?0:-1,"aria-selected":n===e,ref:e=>{u.push(e)},onKeyDown:h,onClick:p,...i,className:(0,s.A)("tabs__item","tabItem_e1w9",i?.className,{"tabs__item--active":n===e}),children:t??e},e))})}function u({children:e}){return(0,r.jsx)("div",{className:"margin-top--md",children:e})}function d({className:e,children:n}){return(0,r.jsxs)("div",{className:(0,s.A)(i.G.tabs.container,"tabs-container","tabList_mVW_"),children:[(0,r.jsx)(c,{className:e}),(0,r.jsx)(u,{children:n})]})}function p(e){let n=(0,a.A)(),t=(0,o.OC)(e);return(0,r.jsx)(o.O_,{value:t,children:(0,r.jsx)(d,{className:e.className,children:(0,o.vT)(e.children)})},String(n))}},28226(e,n,t){t.d(n,{OC:()=>p,O_:()=>g,uc:()=>x,vT:()=>u});var r=t(65723),s=t(22155),i=t(62934),o=t(24127),l=t(12910),a=t(22399),c=t(6660);function u(e){return s.Children.toArray(e).filter(e=>"\n"!==e)}function d({value:e,tabValues:n}){return n.some(n=>n.value===e)}function p(e){let n,{defaultValue:t,queryString:r=!1,groupId:u}=e,p=function(e){let{values:n,children:t}=e;return(0,s.useMemo)(()=>{let e=n??s.Children.toArray(t).flatMap(e=>{if(!e)return[];if((0,s.isValidElement)(e)&&function(e){let{props:n}=e;return!!n&&"object"==typeof n&&"value"in n}(e))return[e];
1let n="string"==typeof e.type?e.type:e.type.name;throw Error(`Docusaurus error: Bad <Tabs> child <${n}>: all children of the <Tabs> component should be <TabItem>, and every <TabItem> should have a unique "value" prop. 2If you do not want to pass on a "value" prop to the direct children of <Tabs>, you can also pass an explicit <Tabs values={...}> prop.`)}).map(({props:{value:e,label:n,attributes:t,default:r}})=>({value:e,label:n,attributes:t,default:r})),r=(0,a.XI)(e,(e,n)=>e.value===n.value);if(r.length>0)throw Error(`Docusaurus error: Duplicate values "${r.map(e=>`'${e.value}'`).join(", ")}" found in <Tabs>. Every value needs to be unique.`);return e},[n,t])}(e),[h,x]=(0,s.useState)(()=>(function({defaultValue:e,tabValues:n}){if(0===n.length)throw Error("Docusaurus error: the <Tabs> component requires at least one <TabItem> children component");if(e){if(!d({value:e,tabValues:n}))throw Error(`Docusaurus error: The <Tabs> has a defaultValue "${e}" but none of its children has the corresponding value. Available values are: ${n.map(e=>e.value).join(", ")}. If you intend to show no default tab, use defaultValue={null} instead.`);return e}let t=n.find(e=>e.default)??n[0];if(!t)throw Error("Unexpected error: 0 tabValues");return t.value})({defaultValue:t,tabValues:p})),[g,y]=function({queryString:e=!1,groupId:n}){let t=(0,i.W6)(),r=function({queryString:e=!1,groupId:n}){if("string"==typeof e)return e;if(!1===e)return null;if(!0===e&&!n)throw Error('Docusaurus error: The <Tabs> component groupId prop is required if queryString=true, because this value is used as the search param name. You can also provide an explicit value such as queryString="my-search-param".');return n??null}({queryString:e,groupId:n});return[(0,l.aZ)(r),(0,s.useCallback)(e=>{if(!r)return;let n=new URLSearchParams(t.location.search);n.set(r,e),t.replace({...t.location,search:n.toString()})},[r,t])]}({queryString:r,groupId:u}),[m,f]=function({groupId:e}){let n=e?`docusaurus.tab.${e}`:null,[t,r]=(0,c.Dv)(n);return[t,(0,s.useCallback)(e=>{n&&r.set(e)},[n,r])]}({groupId:u}),j=d({value:n=g??m,tabValues:p})?n:null;return(0,o.A)(()=>{j&&x(j)},[j]),{selectedValue:h,selectValue:(0,s.useCallback)(e=>{if(!d({value:e,tabValues:p}))throw Error(`Can't select invalid tab value=${e}`);x(e),y(e),f(e)},[y,f,p]),tabValues:p,lazy:e.lazy??!1,block:e.block??!1}}let h=(0,s.createContext)(null);function x(){let e=s.useContext(h);if(!e)throw Error("useTabsContext() must be used within a Tabs component");return e}function g(e){return(0,r.jsx)(h.Provider,{value:e.value,children:e.children})}},7143(e,n,t){t.d(n,{R:()=>o,x:()=>l});var r=t(22155);let s={},i=r.createContext(s);function o(e){let n=r.useContext(i);return r.useMemo(function(){return"function"==typeof e?e(n):{...n,...e}},[n,e])}function l(e){let n;return n=e.disableParentContext?"function"==typeof e.components?e.components(s):e.components||s:o(e.components),r.createElement(i.Provider,{value:n},e.children)}}}]);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.