Skip to content

Array.prototype.includes should be narrow on union of enum types instead of throwing #49906

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
5 tasks done
mtsoltan opened this issue Jul 14, 2022 · 3 comments
Closed
5 tasks done
Labels
Duplicate An existing issue was already created

Comments

@mtsoltan
Copy link

Suggestion

🔍 Search Terms

includes, enum (#46085 is relevant but not what this is discussing)

✅ 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

Array.prototype.includes should be able to narrow down on a union of enum types instead of throwing:

TS2345: Argument of type 'SomeEnum | SomeOtherEnum' is not assignable to parameter of type 'SomeEnum'.   Type 'SomeOtherEnum.C' is not assignable to type 'SomeEnum'.

📃 Motivating Example

export enum SomeEnum {
    A = 'aaa',
    B = 'bbb'
}

export enum SomeOtherEnum {
    C = 'ccc',
    D = 'ddd'
}


  function someFunction(somePossibleValue: SomeEnum | SomeOtherEnum) {
    if (Object.values(SomeEnum).includes(somePossibleValue)) { // <--- Includes signature should be changed so this narrow the type at compile time
      return 'some result'; // <--- Inside the if-body, the type of somePossibleValue should be successfully narrowed down
    }
    if (Object.values(SomeOtherEnum).includes(somePossibleValue)) { // <--- Includes signature should be changed so this narrow the type at compile time
      return 'some other result'; // <--- Inside the if-body, the type of somePossibleValue should be successfully narrowed down
    }

    throw new Error('On neither'); // <--- I understand this cannot be figured out at compile time because .includes cannot execute at compile time
  }

💻 Use Cases

Allowing checks on something that could be possibly one of multiple enums.

@MartinJohns
Copy link
Contributor

MartinJohns commented Jul 14, 2022

Duplicate of #26255 / #14520 and also #36275.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Jul 14, 2022
@mtsoltan
Copy link
Author

🤦 I should've searched more before opening an issue. Apparently this has more to do with Array.prototype.includes in general, and not just with how it acts on enums. Feel free to close if you feel this would fork the discussion to pointless directions.

@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants