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

[prefer-readonly-parameter-types] Using prefer-readonly-parameter-types rule with lib.dom #1727

Closed
denis-afanasev opened this issue Mar 13, 2020 · 1 comment
Labels
package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin working as intended Issues that are closed as they are working as intended

Comments

@denis-afanasev
Copy link

I can't figure out how to use prefer-readonly-parameter-types rule on interop with DOM (lib.dom typings).

Previously i using @typescript-eslint/eslint-plugin: ^2.20.0 and have code like this:

class SomeClass {
...
    public someFunc(element: HTMLElement): void {
        ...
    }
}

After update to 2.23.0 i got a eslint error
Parameter should be a read only typeeslint(@typescript-eslint/prefer-readonly-parameter-types)
This rule require something like

class SomeClass {
...
    public someFunc(element: Readonly<HTMLElement>): void {
        ...
    }
}

After that i got next error on the same place Make all properties in T readonly.
It means that i should modify external library's code to fix that error, but it's impossible

I think that lib.dom (HTMLElement, etc) just 1 out of many errors from this rule on project-library interop layer.

This error caused by #1513
So I suggest to switch rule level from error to warn in packages/eslint-plugin/src/configs/all.json

@denis-afanasev denis-afanasev added package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for maintainers to take a look labels Mar 13, 2020
@bradzacher
Copy link
Member

bradzacher commented Mar 13, 2020

See #1668
This is intentional - the rule enforces that objects are deeply readonly, not just surface readonly.

Ie HTMLElement et al are not deeply readonly, even with Readonly<>.
As this code is valid:

declare const x: Readonly<HTMLElement>;
x.classList = 1; // error assigning to readonly property
x.classList.foo = 1; // no readonly error, as `classList` is not readonly.

This is listed in the rule's docs:
https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/prefer-readonly-parameter-types.md#rule-details

A type is considered readonly if:
...

  • it is an object type whose properties are all marked as readonly, and whose values are all considered readonly.

You will need to use a deep readonly type for these instances.

@bradzacher bradzacher added working as intended Issues that are closed as they are working as intended and removed triage Waiting for maintainers to take a look labels Mar 13, 2020
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 20, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin working as intended Issues that are closed as they are working as intended
Projects
None yet
Development

No branches or pull requests

2 participants