💼🚫 This rule is enabled in the 🎨 stylistic
config. This rule is disabled in the disableTypeChecked
config.
🔧 This rule is automatically fixable by the --fix
CLI option.
💭 This rule requires type information.
This rule enforces consistently using either readonly
keywords or Readonly<T>
.
There are two ways to declare type literals as readonly, either by specifying that each
property of the type is readonly using the readonly
keyword, or by wrapping the type
in Readonly
.
This rule is designed to enforce a consistent way of doing this.
/* eslint functional/readonly-type: ["error", "keyword"] */
type Foo = Readonly<{
bar: string;
baz: number;
}>;
/* eslint functional/readonly-type: ["error", "generic"] */
type Foo = {
readonly bar: string;
readonly baz: number;
};
/* eslint functional/readonly-type: ["error", "keyword"] */
type Foo = {
readonly bar: string;
readonly baz: number;
};
type Foo2 = {
readonly bar: string;
baz: number;
};
/* eslint functional/readonly-type: ["error", "generic"] */
type Foo = Readonly<{
bar: string;
baz: number;
}>;
// No issue as it's not fully readonly.
type Foo2 = {
readonly bar: string;
baz: number;
};
This rule takes a single string option, either generic
| keyword
.
const defaults = "generic";
Enforce using Readonly<T>
instead of marking each property as readonly with the readonly
keyword.
Enforce using readonly
keyword for each property instead of wrapping with Readonly
.