# Only allow enumeration on plain object (`ts-compat/prefer-enum-plain-object`) 💼 This rule is enabled in the ✅ `recommended` config. `for...in`, `Object.keys`, `Object.getOwnPropertyNames` and `Object.getOwnPropertyDescriptors` read the string keys of a value. On anything but a plain object they answer a question nobody asked: on an array they hand back the indexes as strings, and on a class instance, on `Map`, `Date` or on a DOM node they return nothing at all, because the data lives behind the prototype. Iterate an array with `for...of`, read a `Map` with `forEach`, and keep these four APIs for object literals, `Record` and data interfaces. The rule reports when the type of the enumerated value can not be a plain object: an array or a tuple, a class instance, an interface of the TypeScript standard library such as `Map`, `Promise` or `HTMLElement`, a function, or a primitive. `any`, `unknown` and a union which still holds a plain object are left alone. The browserslist config enables the rule by the support of `Object.getOwnPropertyDescriptors`, the least supported of the four: it needs Chrome 54, Edge 15, Firefox 50, Safari 10, Opera 41 or iOS Safari 10, and no version of IE has it. ## Rule Details Examples of **incorrect** code for this rule: ```ts var arr = [1, 2, 3]; for (var key in arr) { } Object.keys(arr); var map = new Map(); Object.getOwnPropertyNames(map); var date = new Date(); Object.getOwnPropertyDescriptors(date); ``` Examples of **correct** code for this rule: ```ts var plain = { a: 1, b: 2 }; for (var key in plain) { } Object.keys(plain); var record: Record = {}; Object.getOwnPropertyNames(record); var partial: Partial<{ a: number }> = {}; Object.getOwnPropertyDescriptors(partial); ``` ## When Not To Use It * The enumerated value is a string keyed dictionary which the types only describe as something else. * The code walks a host object on purpose, such as a debug dump of `window`. ## Further Reading * [MDN for...in](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in) * [MDN Object.keys](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys)