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

[no-type-alias] Support enforcing the inverse #142

Closed
sindresorhus opened this issue Jan 26, 2019 · 8 comments
Closed

[no-type-alias] Support enforcing the inverse #142

sindresorhus opened this issue Jan 26, 2019 · 8 comments
Labels
breaking change This change will require a new major version to be released enhancement: plugin rule option New rule option for an existing eslint-plugin rule has pr there is a PR raised to close this package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin
Milestone

Comments

@sindresorhus
Copy link

sindresorhus commented Jan 26, 2019

Similar to my #103 issue.

The no-type-alias rule enforces not using type aliases, but I would like to enforce use of type aliases whenever possible. I actually prefer them, and most of the type alias limitations that existed when the original TSLint interface-over-type-literal rule was created has since gone away.

I would suggest renaming the rule to type-alias (without the no- prefix) and support enforcing both use and non-use of type alias.

Relevant:

In short, the only differences are:

  1. A class cannot implement / extend a type alias that names a union type.
  2. Declaration merging doesn't work with type aliases.

Personally, I would like to use enforce type aliases whenever possible, which means all the time, except for number 1.. I don't use declaration merging.

I also noticed there's a prefer-interface rule which seems to overlap with this one. It should probably be merged into this one.

@sindresorhus sindresorhus added package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for maintainers to take a look labels Jan 26, 2019
@armano2
Copy link
Member

armano2 commented Jan 26, 2019

i'm unsure how enforcing of type aliases should work and how to determine need for type alias

whenever possible: where do you think it should be?

@sindresorhus
Copy link
Author

sindresorhus commented Jan 26, 2019

whenever possible: where do you think it should be?

All the time, except for this case:

A class cannot implement / extend a type alias that names a union type.

Assuming the above can be detected.

Or better said, I want to use type aliases as long as the compiler permits it.

@bradzacher
Copy link
Member

After spending 5 months working with flow, I've gotten used to writing type statements for everything.
I was thinking that I needed something like this!

The usages part might be hard though because we'd have to track the scope. Doable, but ugly.

Depending on complexity it might just be worth having people use disable comments for the rare cases they use interfaces.

@bradzacher bradzacher added enhancement: plugin rule option New rule option for an existing eslint-plugin rule breaking change This change will require a new major version to be released and removed triage Waiting for maintainers to take a look labels Jan 26, 2019
@braposo
Copy link

braposo commented Apr 11, 2019

I found this issue while searching for exactly the same thing, although I have a different suggestion.

The no-type-alias is used to define the rules that determine how the type aliases can be implemented in the code: from not allowed at all to only in specific cases like unions and intersections.

On the other hand, the prefer-interface option forces the use of interfaces over type aliases.

I think that the prefer-interface rule is more suitable for this change and would be just a matter of adding the option to select "always" (the default and current behaviour) or "never" (the new option that would force to avoid interfaces).

It wouldn't have any breaking change because the default behaviour would still be the same as we have now, but there would be an option to force the inverse and prefer type aliases over interfaces.

This is something that other rules already have and wouldn't require a lot of different variations to consider like we would have if updating the no-type-alias rule.

@otofu-square
Copy link
Contributor

otofu-square commented Apr 23, 2019

+1
I tried to create the PoC for this.
https://github.com/otofu-square/eslint-plugin-prefer-type-alias

@bradzacher
Copy link
Member

@otofu-square - how come you didn't fork this repo and make you changes in your fork?
We're happy to have (and hugely encourage) new contributions!
If you'd worked within this codebase, you'd have had full types for everything (i.e. no anys)!

There isn't a technical limitation for us not yet making the change, it's a manpower issue. There are only a small number of core maintainers.

@otofu-square
Copy link
Contributor

@bradzacher
Thanks for your comment.
Sounds great.

I'll try to make a PR to this repository.

@bradzacher
Copy link
Member

This was fixed in #463
There is a new rule consistent-type-definitions which lets you choose what type definition style to use

@typescript-eslint typescript-eslint locked as resolved and limited conversation to collaborators Feb 21, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
breaking change This change will require a new major version to be released enhancement: plugin rule option New rule option for an existing eslint-plugin rule has pr there is a PR raised to close this package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin
Projects
None yet
Development

No branches or pull requests

5 participants