Skip to content

TypeScript · correctness

prefer-immutable-module-constant

eslint:prefer-immutable-module-constant

Require module-level constant collections to expose readonly state.

Default
error
Fix
none
Languages
typescript

Why

A const binding prevents reassignment but does not stop callers from mutating its array, object, Set, or Map contents.

Fix

Expose literals with `as const` or a readonly type, and expose Set or Map values through ReadonlySet or ReadonlyMap.

Before / after

Executed by this rule’s unit tests.

Before

A module constant exposes a mutable array

src/constants.ts · focus
const VALUES = [1, 2, 3];

After

A module constant exposes a readonly literal

src/constants.ts · focus
const VALUES = [1, 2, 3] as const;

Limits

  • The rule skips generated files, test files, JavaScript files, and collections that are deliberately mutated in their declaring module.

Message IDs

preferAsConstpreferReadonlyCollection