Skip to main content

prefer-ts-extras-object-values

Prefer objectValues from ts-extras over Object.values(...).

objectValues(...) preserves stronger value typing and keeps value iteration contracts explicit.

Targeted pattern scopeโ€‹

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

  • Object.values(value) call sites that can use objectValues(value).

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

What this rule reportsโ€‹

This rule reports Object.values(value) call sites when objectValues(value) is the intended replacement.

  • Object.values(value) call sites that can use objectValues(value).

Why this rule existsโ€‹

objectValues improves value typing during iteration and post-processing.

  • Value unions are preserved more consistently.
  • Downstream map/filter code needs fewer local casts.
  • Value extraction style stays consistent across modules.

โŒ Incorrectโ€‹

const values = Object.values(siteStateMap);

โœ… Correctโ€‹

const values = objectValues(siteStateMap);

Behavior and migration notesโ€‹

  • Runtime semantics align with Object.values (own enumerable string-keyed values).
  • Symbol-keyed values remain excluded, matching native behavior.
  • For broadly typed records, resulting value types remain broad.

Additional examplesโ€‹

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

const values = Object.values(features);

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

const values = objectValues(features);

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

const labels = objectValues(enumLikeObject);

ESLint flat config exampleโ€‹

import typefest from "eslint-plugin-typefest";

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

When not to use itโ€‹

Disable this rule if direct Object.values calls are required for interop constraints.

Package documentationโ€‹

ts-extras package documentation:

Source file: source/object-values.ts

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

This is useful since `Object.values()` always returns `T[]`. This function returns a strongly-typed array of the values of the given object.

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

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

const object: {a: number; b?: string} = {a: 1, b: 'hello'};

const stronglyTypedValues = objectValues(object);
//=> Array<number | string>

const untypedValues = Object.values(object);
//=> Array<string | number | undefined>
```

@category Improved builtin
*/

Rule catalog ID: R031

Further readingโ€‹

Adoption resourcesโ€‹