Skip to main content

prefer-ts-extras-object-from-entries

Prefer objectFromEntries from ts-extras over Object.fromEntries(...).

objectFromEntries(...) preserves stronger key/value typing and avoids local casting after entry reconstruction.

Targeted pattern scopeโ€‹

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

  • Object.fromEntries(entries) call sites that can use objectFromEntries(entries).

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

What this rule reportsโ€‹

This rule reports Object.fromEntries(entries) call sites when objectFromEntries(entries) is the intended replacement.

  • Object.fromEntries(entries) call sites that can use objectFromEntries(entries).

Why this rule existsโ€‹

objectFromEntries improves the reconstructed object type when building objects from typed entry tuples.

  • Reconstructed key/value relationships are preserved more consistently.
  • Follow-up casting after reconstruction is needed less often.
  • Object reconstruction uses one explicit helper across modules.

โŒ Incorrectโ€‹

const statusById = Object.fromEntries(statusEntries);

โœ… Correctโ€‹

const statusById = objectFromEntries(statusEntries);

Behavior and migration notesโ€‹

  • Runtime semantics align with Object.fromEntries.
  • Duplicate keys keep the last assigned entry, matching native behavior.
  • Input must still be iterable entry pairs ([key, value]).
  • If your entries are already widened to [string, unknown], resulting object types remain broad.

Additional examplesโ€‹

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

const config = Object.fromEntries(entries);

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

const config = objectFromEntries(entries);

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

const grouped = objectFromEntries(pairs);

ESLint flat config exampleโ€‹

import typefest from "eslint-plugin-typefest";

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

When not to use itโ€‹

Disable this rule if you must keep direct Object.fromEntries calls for interop or platform constraints.

Package documentationโ€‹

ts-extras package documentation:

Source file: source/object-from-entries.ts

/**
A strongly-typed version of `Object.fromEntries()`.

This is useful since `Object.fromEntries()` always returns `{[key: string]: T}`. This function returns a strongly-typed object from the given array of entries.

- [TypeScript issues about this](https://github.com/microsoft/TypeScript/issues/35745)

@example
```
import {objectFromEntries} from 'ts-extras';

const stronglyTypedObjectFromEntries = objectFromEntries([
['a', 123],
['b', 'someString'],
['c', true],
]);
//=> {a: number; b: string; c: boolean}

const untypedEntries = Object.fromEntries([['a', 123], ['b', 'someString'], ['c', true]]);
//=> {[key: string]: unknown}
```

@category Improved builtin
*/

Rule catalog ID: R027

Further readingโ€‹

Adoption resourcesโ€‹