typescript-eslint: [method-signature-style] Shouldn't affect methods that use `this`

Repro

{
  "rules": {
    "@typescript-eslint/method-signature-style": ["error"]
  }
}
interface Foo {
	cloneThis(): this; // ❌ reported
	cloneThis: () => this; //  🤔 fixed to this
}

Expected Result

Methods should not be affected, this should not be autofixed:

interface Foo {
	cloneThis(): this;
}

Actual Result

That type is changed to an arrow function, which doesn’t have this

interface Foo {
	cloneThis: () => this; //  Fixed to this, broken
}

Additional Info

It causes errors elsewhere in the code

Versions

package version
@typescript-eslint/eslint-plugin 2.27.0
@typescript-eslint/parser 2.27.0
TypeScript 3.8.3
ESLint 6.8.0
node 13.8.0
npm 6.13.6

About this issue

  • Original URL
  • State: open
  • Created 7 years ago
  • Reactions: 3
  • Comments: 17 (15 by maintainers)

Commits related to this issue

Most upvoted comments

Based on the complexity of getting the this-scoping correct in nested function scenarios (mostly discussed under https://github.com/typescript-eslint/typescript-eslint/issues/10609), maybe a reasonable resolution is that we just disable the autofix for any signatures that contain TSThisType as a descendant, even if it’s technically referring to an inner this, to avoid depending on significant scope manager work (https://github.com/typescript-eslint/typescript-eslint/issues/10609#issuecomment-2663197925)?

@typescript-eslint/triage-team

I still don’t fully agree - just because a suggested fix may break the code doesn’t mean it’s the wrong thing to do.

For example suggesting someone doesn’t use any is a good thing and more often than not will lead to much better code - even if the immediate act of removing the any maybe cause type errors.

Instead if you change your statement to say “A linter should not autofix a piece of code that is correct (according to the compiler) to something that breaks it.” - then I fully agree! Autofixes shouldn’t break the build.

In this instance the suggestion isn’t necessarily wrong to use a function type as its got a safer variance and (barring the subtype example I mentioned earlier) there is an alternative that can be used without introducing an error. But it’s quite hard for us to detect the subtype case thus in this instance it’s probably better if we just ingore this case.

The action would be:

  1. If the signature includes a this parameter - do not fix it (but still report).
  2. Perhaps update the error message in this case to describe the edge case?
  3. Document the edge case in the rule doc.

A linter should not suggest to change the behavior of a piece of code that is correct (according to the compiler)

@fregante this is an incorrect take.

foo!!!!!!!!!!!!

This code is correct according to the compiler. But clearly it’s bad code. Should a rule (a) ignore it or (b) report and fix it to foo!

What about if the code is this:

const foo = 1;
foo!.toString();

Again this is fine according to TS - it doesn’t care if you’ve got provably useless code. But the correct code is to drop the useless non-null assertion.

This same thing can be extrapolated to all of our rules really. Just because TS thinks something is correct doesn’t mean it’s correct or safe code. The point of the linter is to augment TS’s checks - provide extra checks that TS doesn’t do.

Based on our analysis we can find and flag code that looks wrong even if the types say there is no issue. We can also suggest patterns which are more safe and work around unsoundness in TS.

The latter is what this rule does - the variance of method signatures is bad and function types are generally safer.

Bringing this back from the dead- I do think there’s a missing, very useful feature here. The weirdness is caused by an intentional difference in the strictness of assignments between methods and properties

interface WithMethod {
  getName(animal: Dog): string
}

interface WithProperty {
  getName: (animal: Dog) => string
}

declare const withMethod: WithMethod
declare const withProperty: WithProperty

const arbitraryAssignment: {
  getName (animal: Animal) => string
} = withMethod // doesn't throw, but does with withProperty. The second behavior is better

In TS it is also possible to express function signatures with restrictions on the this type:

interface Dog {
  name: string
  getName(this: Dog): string
}

I think that it would be useful to allow the method syntax for functions that specify a this type in the parameters slot even when the rule is configured to only allow property signatures more broadly. There are cases where I might want to define types for method-like behavior even if in general property-style signatures give stricter checks

Hmm - this is weird. I’m not sure why TS treats the two differently.

In essence the two signatures are actually the same - both define a function that returns this. Whilst it looks like an arrow function - that syntax is just a coincidence, and the function type has no relation to an arrow function whatsoever.

I suspect that this is either:

  • a bug in TS
  • an intentional difference due to the difference in variance of the two signatures

Unsure. I’d need to do more investigation (I always forget how variance works exactly)