Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New option ignore-interface-prefix on readonly-keyword rule #123

Open
jonaskello opened this issue Mar 8, 2019 · 3 comments
Open

New option ignore-interface-prefix on readonly-keyword rule #123

jonaskello opened this issue Mar 8, 2019 · 3 comments

Comments

@jonaskello
Copy link
Owner

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>;
@jonaskello
Copy link
Owner Author

Perhaps ignore-interface-prefix is not a good name as this would apply to type in addition to interface.

@geon
Copy link
Contributor

geon commented Mar 29, 2019

ignore-type-prefix would be a better name, since interfaces and classes are types too.

@geon
Copy link
Contributor

geon commented Mar 29, 2019

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants