Skip to main content

prefer-ts-extras-object-entries

Prefer objectEntries from ts-extras over Object.entries(...).

objectEntries(...) preserves stronger key/value typing for object iteration and reduces local casting noise.

Targeted pattern scopeโ€‹

This rule focuses on direct Object.entries(value) calls that can be migrated to objectEntries(value) with deterministic fixes.

  • Object.entries(value) call sites that can use objectEntries(value).

Alias indirection, wrapper helpers, and non-canonical call shapes are excluded to keep objectEntries(value) migrations safe.

What this rule reportsโ€‹

This rule reports Object.entries(value) call sites when objectEntries(value) is the intended replacement.

  • Object.entries(value) call sites that can use objectEntries(value).

Why this rule existsโ€‹

objectEntries gives better static typing for entry iteration, especially when the object has known keys.

  • Key unions are preserved more consistently in loops.
  • Value access in tuple destructuring needs fewer local casts.
  • Team code converges on one explicit runtime helper for entry iteration.

โŒ Incorrectโ€‹

const pairs = Object.entries(siteStatusById);

โœ… Correctโ€‹

const pairs = objectEntries(siteStatusById);

Behavior and migration notesโ€‹

  • Runtime semantics stay aligned with Object.entries (own enumerable string-keyed entries only).
  • Property order behavior remains the same as native Object.entries.
  • Symbol keys are still excluded, just like native behavior.
  • For loosely typed inputs (for example Record<string, unknown>), key type remains broad (string).

Additional examplesโ€‹

โŒ Incorrect โ€” Additional exampleโ€‹

const entries = Object.entries(settings);

โœ… Correct โ€” Additional exampleโ€‹

const entries = objectEntries(settings);

โœ… Correct โ€” Repository-wide usageโ€‹

for (const [key, value] of objectEntries(env)) {
void key;
void value;
}

ESLint flat config exampleโ€‹

import typefest from "eslint-plugin-typefest";

export default [
{
plugins: { typefest },
rules: {
"typefest/prefer-ts-extras-object-entries": "error",
},
},
];

When not to use itโ€‹

Disable this rule if you must use native Object.entries directly for interop constraints.

Package documentationโ€‹

ts-extras package documentation:

Source file: source/object-entries.ts

/**
* A strongly-typed version of `Object.entries()`.
*
* This is useful since `Object.entries()` always returns an array of
* `Array<[string, T]>`. This function returns a strongly-typed array of the
* entries of the given object.
*
* - [TypeScript issues about
* this](https://github.com/microsoft/TypeScript/pull/12253)
*
* @category Improved builtin
*
* @example
* ```
* import {objectEntries} from 'ts-extras';
*
* const stronglyTypedEntries = objectEntries({a: 1, b: 2, c: 3});
* //=> Array<['a' | 'b' | 'c', number]>
*
* const untypedEntries = Object.entries({a: 1, b: 2, c: 3});
* //=> Array<[string, number]>
* ```;
*/

Rule catalog ID: R026

Further readingโ€‹

Adoption resourcesโ€‹