prefer-ts-extras-object-keys
Prefer objectKeys from ts-extras over Object.keys(...).
objectKeys(...) preserves stronger key typing and avoids repeated casts in iteration paths.
Targeted pattern scopeโ
This rule focuses on direct Object.keys(value) calls that can be migrated to objectKeys(value) with deterministic fixes.
Object.keys(value)call sites that can useobjectKeys(value).
Alias indirection, wrapper helpers, and non-canonical call shapes are excluded to keep objectKeys(value) migrations safe.
What this rule reportsโ
This rule reports Object.keys(value) call sites when objectKeys(value) is the intended replacement.
Object.keys(value)call sites that can useobjectKeys(value).
Why this rule existsโ
objectKeys improves key typing for indexed access and iteration paths.
- Fewer
as Array<keyof T>casts in loops. - Safer indexed reads after key iteration.
- One canonical helper for key iteration patterns.
โ Incorrectโ
const keys = Object.keys(monitorConfig);
โ Correctโ
const keys = objectKeys(monitorConfig);
Behavior and migration notesโ
- Runtime semantics align with
Object.keys(own enumerable string keys only). - Symbol keys are excluded, same as native behavior.
- Numeric keys are still returned as strings, matching native output.
Additional examplesโ
โ Incorrect โ Additional exampleโ
const keys = Object.keys(model);
โ Correct โ Additional exampleโ
const keys = objectKeys(model);
โ Correct โ Repository-wide usageโ
for (const key of objectKeys(theme)) {
void key;
}
ESLint flat config exampleโ
import typefest from "eslint-plugin-typefest";
export default [
{
plugins: { typefest },
rules: {
"typefest/prefer-ts-extras-object-keys": "error",
},
},
];
When not to use itโ
Disable this rule if you must keep direct Object.keys calls for interop constraints.
Package documentationโ
ts-extras package documentation:
Source file: source/object-keys.ts
/**
A strongly-typed version of `Object.keys()`.
This is useful since `Object.keys()` always returns an array of strings. This function returns a strongly-typed array of the keys of the given object.
- [Explanation](https://stackoverflow.com/questions/55012174/why-doesnt-object-keys-return-a-keyof-type-in-typescript)
- [TypeScript issues about this](https://github.com/microsoft/TypeScript/issues/45390)
@example
```
import {objectKeys} from 'ts-extras';
const stronglyTypedItems = objectKeys({a: 1, b: 2, c: 3}); // => Array<'a' | 'b' | 'c'>
const untypedItems = Object.keys({a: 1, b: 2, c: 3}); // => Array<string>
```
@category Improved builtin
@category Type guard
*/
Rule catalog ID: R030