microsoft / TypeScript

TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
https://www.typescriptlang.org
Apache License 2.0
100.82k stars 12.46k forks source link

Prevent union of enums with same values (non-narrowable unions) #46085

Open georeith opened 3 years ago

georeith commented 3 years ago

Suggestion

🔍 Search Terms

✅ Viability Checklist

My suggestion meets these guidelines:

⭐ Suggestion

I would like a configurable option to prevent the union of enums with same values.

📃 Motivating Example

Consider the following example:

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:

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

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.

sandersn commented 3 years ago

It cannot be an error to union two different types, so the error would have to be at the use site. Can you propose when and where the error should be issued?

Two more things:

  1. Is this error something that every programmer would want on every code base? If not, it should be a lint rule.
  2. This surely would cause errors on some existing code. How common is this in existing code?

You could also avoid your particular problem if enum values of different types were not assignable to each other. I believe string enums behave this way.