Closed danielnixon closed 1 year ago
In the meantime, let's configure this override: https://github.com/eslint-functional/eslint-plugin-functional/blob/main/docs/rules/settings/immutability.md#example-of-configuring-immutability-overrides
If we did go down the path of creating a copy of of the ReadonlyArray type by hand, that would be the time to solve this too https://github.com/agiledigital/readonly-types/issues/7 .
Other builtins/primitives are similarly "ignored" by Readonly
. For example, these all compile:
type Foo = Readonly<number>;
const a: Foo = 1;
// eslint-disable-next-line functional/no-expression-statements, functional/immutable-data, functional/functional-parameters
a.toString = () => "";
type Foo = Readonly<bigint>;
const a: Foo = BigInt(1);
// eslint-disable-next-line functional/no-expression-statements, functional/immutable-data, functional/functional-parameters
a.toString = () => "";
Oh, the answer was right in front of my eyes, courtesy of is-immutable-type.
type ImmutableShallow<T extends {}> = {
readonly [P in keyof T & {}]: T[P];
};
ReadonlyArray is mutable for the same reason we provide
ImmutableSet
andImmutableMap
- method syntax.Here's an example - this compiles:
Unfortunately, due to what I assume is a special case rule (because arrays get special treatment by the compiler),
Readonly
does not fix the problem forReadonlyArray
the way it does forReadonlySet
andReadonlyMap
and any other type.This still compiles:
If we want an ImmutableArray (i.e. one that isn't flagged by the type-declaration-immutability eslint rule), we will have to copy all of the
ReadonlyArray
methods from lib.es5.d.ts (and elsewhere probably) and replace them with readonly properties by hand.