Skip to content

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

Open
@georeith

Description

@georeith

Suggestion

πŸ” Search Terms

  • enum
  • union
  • narrowable
  • same values

βœ… Viability Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs ProposalThis issue needs a plan that clarifies the finer details of how it could be implemented.SuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions