Skip to content

[class-literal-property-style] Literal property style does not respect inherited types #3602

Closed
@Paril

Description

@Paril
  • I have tried restarting my IDE and the issue persists.
  • I have updated to the latest version of the packages.
  • I have read the FAQ and my problem is not listed.

Repro

{
  "rules": {
    "@typescript-eslint/class-literal-property-style": "warn"
  }
}
abstract class BaseClass
{
    // class-literal-property-style error; this one is fair, because it isn't entirely aware
    // that I'm going to override it, so this one I can see just being an eslint disable
    get cursor(): string|string[] { return 'crosshair'; }

    readonly hidden: boolean = false;

    abstract get icon(): string;
}

class ChildClass extends BaseClass
{
    // class-literal-property-style error
    // this one, however, is interesting because it should know that it is
    // overriding an existing getter and can't be made a literal 
    override get cursor() { return 'square'; }

    readonly hidden: boolean = true

    readonly icon = 'circle'

    something: boolean
}

Expected Result

ESLint shouldn't suggest class-literal-property-style when overriding a getter, because it is not possible to turn an inherited getter into a readonly property (ts(2610))

Actual Result

It suggests breaking the code by attempting to convert the children into a literal when it is not possible. Conversely, it suggests converting the base class one into a literal, which will not work for children that need to use a getter for conditional return values (ts(2611)).

Additional Info

As a side-issue, this rule is not enum-aware and does not trigger if the literal returned from a getter is an enum value:

enum Test
{
    Value
}

class TestClass
{
    // should trigger here
    get test() { return Test.Value; }
}

Versions

package version
@typescript-eslint/eslint-plugin 8.2.1
@typescript-eslint/parser 8.2.1
TypeScript 4.3.4
ESLint 7.28.0
node 14.8.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    accepting prsGo ahead, send a pull request that resolves this issueenhancement: plugin rule optionNew rule option for an existing eslint-plugin rulelocked due to agePlease open a new issue if you'd like to say more. See https://typescript-eslint.io/contributing.package: eslint-pluginIssues related to @typescript-eslint/eslint-plugin

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions