Skip to content

Rule proposal: disallow duplicate union and intersection types #2738

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
remcohaszing opened this issue Nov 5, 2020 · 10 comments · Fixed by #5728
Closed

Rule proposal: disallow duplicate union and intersection types #2738

remcohaszing opened this issue Nov 5, 2020 · 10 comments · Fixed by #5728
Labels
accepting prs Go ahead, send a pull request that resolves this issue enhancement: new plugin rule New rule request for eslint-plugin package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin

Comments

@remcohaszing
Copy link
Contributor

I would like to propose a new rule: no-duplicate-union-intersection.

This rule reports duplicates in unions and intersections. The autofixer will remove all duplicates, keeping the first one.

The following lines will be reported:

type A = 1 | 2 | 3 | 1;
type B = 'foo' | 'bar' | 'foo';
type C = A | B | A | B;

This will be autofixed to:

type A = 1 | 2 | 3;
type B = 'foo' | 'bar';
type C = A | B;

The rule won’t try to resolve references. This may be an addition for later.

If this is accepted, I’m willing to implement this.

@remcohaszing remcohaszing added package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for team members to take a look labels Nov 5, 2020
@bradzacher
Copy link
Member

I've thought about doing this in the past!
Definitely would be a good rule to have

@bradzacher bradzacher added enhancement: new plugin rule New rule request for eslint-plugin and removed triage Waiting for team members to take a look labels Nov 6, 2020
@kenkozheng
Copy link

Sonarqube has this rule Union and intersection types should not be defined with duplicated elements.
And I think it's a good rule to have in eslint.

Hope to get it soon.

@michaelenglo
Copy link

What is the status of this issue? I'm interested to contribute, perhaps by trying to implement this rule.

@bradzacher
Copy link
Member

Everything in this project is community maintained! This issue has been open for 2 months and nobody's mentioned they're working on it - so it's safe to assume nobody is.

Happy to accept a PR!

@remcohaszing
Copy link
Contributor Author

I started an implementation, but lost interest at some point. The basics are pretty simple, but I can into more and more corner cases. I.e. type literals need to be compared, call signatures need to be compared, nested unions and intersections should be handled.

I might pick it up at some point, but I won’t complain if someone else wants to fix this issue. :)

@michaelenglo
Copy link

Cool! I'll see if I can send something EOW. :)

@remcohaszing
Copy link
Contributor Author

I noticed the sort-type-union-intersection-members rule was added. It might me more worthwile to drop my merge request and reuse some logic of that rule, since basically the difference between the two is that sort-type-union-intersection-members should change the order based on the the comparator result, and no-duplicate-union-intersection should remove one entry if the comparator returns 0.

@JoshuaKGoldberg JoshuaKGoldberg added the accepting prs Go ahead, send a pull request that resolves this issue label Oct 25, 2021
@bradzacher bradzacher changed the title new rule: no-duplicate-union-intersection Rule proposal: disallow duplicate union and intersection types May 3, 2022
@sajikix
Copy link
Contributor

sajikix commented Sep 27, 2022

What is the status of this issue? (Is anyone working on this issue?)
I would like to implement this rule if no one is working on this issue.

@bradzacher
Copy link
Member

Currently open for anyone to work on - just needs a champion to implement it!

@EvgenyOrekhov
Copy link

This is available in eslint-plugin-sonar (no-duplicate-in-composite rule)

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 1, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
accepting prs Go ahead, send a pull request that resolves this issue enhancement: new plugin rule New rule request for eslint-plugin package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants