Skip to content

[consistent-type-definitions] Add option to use interface with methods #3894

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

Closed
3 tasks done
btd1337 opened this issue Sep 17, 2021 · 3 comments · Fixed by #3899
Closed
3 tasks done

[consistent-type-definitions] Add option to use interface with methods #3894

btd1337 opened this issue Sep 17, 2021 · 3 comments · Fixed by #3899
Labels
bug Something isn't working package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin

Comments

@btd1337
Copy link

btd1337 commented Sep 17, 2021

It is possible to add the possibility of not validating this rule when the interface has functions (default Interface style used in languages like Java)?

E.G.:

"@typescript-eslint/consistent-type-definitions": ["error", { "type", => "allowInterfaceWithFunctions" <= }]
  • 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

  • Create one interface with methods (Java and C# style)
interface Test {
   bar(): string;
   foo(): number;
}
  • Linter will inform you to use Type

  • Run eslint with --fix

  • Code will be updated to:

type Test = {
   bar(): string;
   foo(): number;
}
  • Eslint will report error: ',' expected

Rule

{
  "rules": {
    "@typescript-eslint/consistent-type-definitions": ["error", "type"],
  }
}

Before

Captura de Tela 2021-09-16 às 22 37 59

After auto fix

Captura de Tela 2021-09-16 às 22 37 37

Expected Result

Don't suggest Type for Interfaces that contain functions (like Java and C# Interface styles)

Actual Result

Suggests change, performs auto-fix, but displays error after it.

Additional Info

Versions

package version
@typescript-eslint/eslint-plugin 4.31.1
@typescript-eslint/parser 4.31.1
TypeScript 4.4.3
ESLint 7.32.0
node 12.18.3
@btd1337 btd1337 added package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for team members to take a look labels Sep 17, 2021
@bradzacher
Copy link
Member

Terminating a type property with a semicolon is valid syntax whether or not it is a type declaration or an interface.

The bug here is not that.
The bug here is that the fix ignored the fact that the interface was export default. You cannot directly export default a type declaration like you can an interface.

Thus the fixer should have instead done something like

type T = {...};
export default T;

@bradzacher bradzacher added bug Something isn't working and removed triage Waiting for team members to take a look labels Sep 17, 2021
@btd1337
Copy link
Author

btd1337 commented Sep 18, 2021

@bradzacher Yes, the one you said is the bug.

But besides the bug, I would like to know about the possibility of having a property that ignores the request to change from interface to type, when the interface only has functions (Java/C# interface style).

E.G.:

"@typescript-eslint/consistent-type-definitions": ["error", { "type", => "allowInterfaceWithFunctions" <= }]

Something like "allowInterfaceWithFunctions" or "ignoreInterfaceWithFunctions"

@bradzacher
Copy link
Member

No chance.
There's no reason that the rule should ignore them aside from a purely stylistic choice made by your codebase.
With these stylistic lint rules we don't seek to satisfy all use cases because we'll never be able to. Instead we seek to support a well known, common subset.

If this is a style you want to use in your codebase - we purposely have a very permissive licence to allow people to fork out code.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 21, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin
Projects
None yet
2 participants