Skip to content
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

Allow const assertion to be use on enum types #30690

Closed
5 tasks done
dragomirtitian opened this issue Apr 1, 2019 · 3 comments · Fixed by #30700
Closed
5 tasks done

Allow const assertion to be use on enum types #30690

dragomirtitian opened this issue Apr 1, 2019 · 3 comments · Fixed by #30700
Labels
Help Wanted You can do this Suggestion An idea for TypeScript
Milestone

Comments

@dragomirtitian
Copy link
Contributor

Search Terms

A 'const' assertion can only be applied to a string, number, boolean, array, or object literal

Suggestion

Allow const assertion to be use on enum types. Currently the const assertion can be applied to string and number literals to get the compiler to keep literal types. But enums, which also can also produce literal types are not allowed in const assertions.

This seems inconsistent.

Use Cases

The use case I wanted to use it for is creating an object that could be assigned to a discriminated union (inspired by this SO question)

Examples

enum AorB { A = 'a', B = 'b' };
const arg = { type: 1 as const }; // { type: 1 }
const arg2 = { type: "1" as const }; // { type: "1" }
const arg3 = { type: AorB.A as const }; // Expected: { type: AorB.A; }, Actual: A 'const' assertion can only be applied to a string, number, boolean, array, or object literal.

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, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.
@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript In Discussion Not yet reached consensus labels Apr 1, 2019
@RyanCavanaugh
Copy link
Member

This wouldn't be a breaking change in existing TypeScript/JavaScript code

Note that as const is recursive, so this would be a potential break

@dragomirtitian
Copy link
Contributor Author

@RyanCavanaugh I don't think it would be because literal types are already inferred for enums in objects. It's just that if the const assertion is directly on the enum we get an error.

const arg3 = { type: AorB.A  } as const // typed as  { readonly type: AorB.A; }
const arg3 = { type: AorB.A as const };  // but error here

@RyanCavanaugh RyanCavanaugh added Help Wanted You can do this and removed In Discussion Not yet reached consensus labels Apr 1, 2019
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Apr 1, 2019
@RyanCavanaugh
Copy link
Member

Accepting PRs for this. It should be allowed only in the case E.P where E is a literal enum; PR should include negative tests for other cases which should be disallowed (e.g. O.P where O is not a literal enum).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Help Wanted You can do this Suggestion An idea for TypeScript
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants