Check out this unique playground.
interface Feature {
readonly name: string;
}
// <Test>
// This is OK
interface Foo1 {
readonly arr: ReadonlyArray<Feature>;
}
function save1(foo: Foo1) {
console.log(foo);
}
// </Test>
// <Test>
// bad? why?
interface Foo2 {
arr: ReadonlyArray<Feature>;
}
function save2(foo: Foo2) {
console.log(foo);
}
// </Test>
// <Test>
// bad? why?
interface Foo3 {
readonly arr: Feature[];
}
function save3(foo: Foo3) {
console.log(foo);
}
// </Test>
I believed that Foo1
, Foo2
, and Foo3
were identical, but according to
@typescript-eslint/prefer-readonly-parameter-types
, they are not. In essence,
Foo1
consists of bothreadonly
andReadonlyArray
Foo2
only containsReadonlyArray
Foo3
solely hasreadonly
What sets these interfaces apart?