Skip to main content

typescript/prefer-readonly-set

Require ReadonlySet instead of Set in type positions.

Targeted pattern scope

This rule targets Set identifier references in TypeScript type references.

What this rule reports

This rule reports Set type references in TypeScript annotations.

Why this rule exists

ReadonlySet makes immutability expectations explicit for API consumers.

❌ Incorrect

function f(values: Set<string>) {}

✅ Correct

function f(values: ReadonlySet<string>) {}

Behavior and migration notes

This rule reports only and does not provide an autofix.

Migration is usually replacing Set<T> with ReadonlySet<T> in type positions.

Options

This rule has no options.

Additional examples

type AllowedRoles = Set<string>;
// ❌ reported

type AllowedRolesView = ReadonlySet<string>;
// ✅ valid

ESLint flat config example

import etcMisc from "eslint-plugin-etc-misc";

export default [
{
plugins: { "etc-misc": etcMisc },
rules: {
"etc-misc/typescript/prefer-readonly-set": "error",
},
},
];

When not to use it

Disable this rule if mutable sets are expected throughout your codebase.

Package documentation

Rule catalog ID: R113

Further reading

Adoption resources

  • Start at warning level in CI, then move to error after cleanup.
  • Use focused codemods/autofix batches per package or directory.