tslint-immutable
tslint-immutable copied to clipboard
New option ignore-interface-prefix on readonly-keyword rule
Today we can ignore prefix on interface members using readonly-keyword with ignore-prefix. Sometimes (for performance reasons) I would like to create an interface where all members are mutable. In those cases it might be helpful to be able to prefix the interface name with something that is ignored (instead of prefixing each member). For example:
interface MutablePerson {
firstName: string
lastName: string
}
Currently readonly-keyword will warn on this interface. My suggestion is that we add an option like ignore-interface-prefix for the readonly-keyword and if I set that to Mutable the above will not warn.
I'm not sure this is a totally good idea since it will be easier to write mutable interfaces using this option. However I find some parts of my code needs to be in fully mutable mode in order to get good performance.
Another way around this is to have a Mutable<T> type mapping that removes the readonly attributes:
type Mutable<T> = { -readonly [P in keyof T]: T[P] };
interface Person {
readonly firstName: string
readonly lastName: string
}
type MutablePerson = Mutable<Person>;
Perhaps ignore-interface-prefix is not a good name as this would apply to type in addition to interface.
ignore-type-prefix would be a better name, since interfaces and classes are types too.
A dedicated type for mutable versions of interfaces is perhaps not very meaningful.
function myFunction(person: MutablePerson){}
vs
function myFunction(person: Mutable<Person>){}
Explicitly using Mutable<Person> also ensures and makes clear that it will always exactly mirror Person. Without context, MutablePerson could be anything.
But it would be great to write something about this solution in the manual, since I'm guessing many users will run into the same issue.
Perhaps a better lint rule would be to prevent Mutable<> in the arguments and return type of functions, but permit it in the function body, just like ignore-local does for arrays.