complete/require-capital-read-only
🚫 This rule is disabled in the ✅ recommended config.
📝 Requires maps/sets/arrays with a capital letter to be read-only.
💭 This rule requires type information.
Rule Details
Section titled “Rule Details”In TypeScript:
- Variables are usually typed with camelCase names.
- Static constants are usually typed with SCREAMING_SNAKE_CASE names.
Thus, when a map, set, array, or object is typed with a capital letter, it is
heavily implied that it is acting as a static constant. Since maps, sets, and
arrays are writable by default, it is common to explicitly type these kinds of
constants with ReadonlyMap<Foo, Bar> or ReadonlySet<Foo> or readonly Foo[]
or Readonly<Foo> to enforce that they can never be modified later on, which
prevents bugs. However, it can be difficult to remember to do this every single
time.
Use this rule to ensure that the SCREAMING_SNAKE_CASE variables are read-only throughout your codebase to keep things as safe as possible.
Also see the
require-capital-const-assertions rule.
// Badconst MY_MAP = new Map([ [1, 2], [3, 4], [5, 6],]);
// Goodconst MY_MAP: ReadonlyMap<number, number> = new Map([ [1, 2], [3, 4], [5, 6],]);
// Badconst MY_SET = new Set([1, 2, 3]);
// Goodconst MY_SET: ReadonlySet<number> = new Set([1, 2, 3]);
// Badconst MY_ARRAY = [1, 2, 3];
// Goodconst MY_ARRAY: readonly number[] = [1, 2, 3];
// Badconst MY_OBJECT = { foo: 123, bar: 456,};
// Goodinterface Foo { foo: number; bar: number;}const MY_OBJECT: Readonly<Foo> = { foo: 123, bar: 456,};Options
Section titled “Options”This rule is not configurable.
