Repository: TypeScript
Issue: Prevent union of enums with same values (non-narrowable unions)
Labels: Suggestion,Needs Proposal

# Suggestion

<!--
  Please fill in each section completely. Thank you!
-->

## 🔍 Search Terms

<!--
  💡 Did you know? TypeScript has over 2,000 open suggestions!
  🔎 Please search thoroughly before logging new feature requests as most common ideas already have a proposal in progress.
  The "Common Feature Requests" section of the FAQ lists many popular requests: https://github.com/Microsoft/TypeScript/wiki/FAQ#common-feature-requests

  Replace the text below:
-->

- enum
- union
- narrowable
- same values

## ✅ Viability Checklist

<!--
   Suggestions that don't meet all these criteria are very, very unlikely to be accepted.
   We always recommend reviewing the TypeScript design goals before investing time writing
   a proposal for ideas outside the scope of the project.
-->
My suggestion meets these guidelines:

* [x] This wouldn't be a breaking change in existing TypeScript/JavaScript code
* [x] This wouldn't change the runtime behavior of existing JavaScript code
* [x] This could be implemented without emitting different JS based on the types of the expressions
* [x] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
* [x] This feature would agree with the rest of [TypeScript's Design Goals](https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals).


## ⭐ Suggestion

I would like a configurable option to prevent the union of enums with same values.

## 📃 Motivating Example

<!--
  If you were announcing this feature in a blog post, what's a short explanation that shows
  a developer why this feature improves the language?
-->

Consider the following example:

```ts
enum HorizontalEdge {
  Left,
  Center,
  Right,
}

enum VerticalEdge {
  Top,
  Center,
  Bottom,
}

type Edge = HorizontalEdge | VerticalEdge; // should warn here if configured

const horizontal = HorizontalEdge.Left; // 0
const vertical = VerticalEdge.Top; // 0

const isHorizontalEdge = (edge: Edge): edge is HorizontalEdge => {
  const horizontalEdges: Edge[] = [
    HorizontalEdge.Left,
    HorizontalEdge.Center,
    HorizontalEdge.Right,
  ];
  return horizontalEdges.includes(edge);
};

isHorizontalEdge(vertical); // true
isHorizontalEdge(horizontal); // true
```

There is no warning from TypeScript that `isHorizontalEdge` may return true when passed a `VerticalEdge`.

This could even occur with string enums if the developer is not careful:

```ts
const enum HorizontalEdge {
  Left = 'left',
  Center = 'center',
  Right = 'right',
}

const enum VerticalEdge {
  Top = 'top',
  Center = 'center',
  Bottom = 'bottom',
}

type Edge = HorizontalEdge | VerticalEdge;
```

Warning against unioning enums with same values would prevent this situation and tell the developer that they need to give their enums distinct values or the union can not be narrowed later as it may lead to runtime errors.

## 💻 Use Cases

<!--
  What do you want to use this for?
  What shortcomings exist with current approaches?
  What workarounds are you using in the meantime?
-->

Currently it is up to the developer to be aware of this and carefully check their enums do not overlap when unioning them into a type (the developer may not even be aware this enum is later used in an union), the configuration operation would allow you to opt out of this behaviour entering your codebase.