Skip to content

Suggestion: const enums should be composable at compile time #18565

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
axefrog opened this issue Sep 19, 2017 · 2 comments
Closed

Suggestion: const enums should be composable at compile time #18565

axefrog opened this issue Sep 19, 2017 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@axefrog
Copy link

axefrog commented Sep 19, 2017

TypeScript Version: 2.5.2

I've found myself using const enums with (figuratively) namespaced property names. There is a lot of duplication when nesting though, and I keep finding myself wishing that the TypeScript compiler could recognise that its compile-time-constant requirement for such values was in fact satisfiable with respect to expressions composed of identifiers that are entirely compile-time resolvable.

Code

const enum Root {
  foo = 'foo';
}

const enum Nested {
  bar = Root.foo + ':bar',
  baz = `${A.foo}:baz`
}

// const strings could be included:

const root = 'root';
const enum Root {
  foo = root + ':foo';
}

// or another idea:

const enum Nested extends Root.foo {
  bar = ':bar' // => 'root:foo:bar'
}

I'm not sure how straightforward this would be to implement, but it does seem to me that if an expression can be resolved at compile time, it should be valid in a const enum type. If the compiler is able to resolve compile-time expressions at all, I imagine that some of the same code might also overlap with other issues relating to use of const symbols in interfaces.

@RyanCavanaugh
Copy link
Member

Duplicate #13969 ?

@mhegazy mhegazy added the Duplicate An existing issue was already created label Sep 19, 2017
@mhegazy
Copy link
Contributor

mhegazy commented Oct 4, 2017

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

@mhegazy mhegazy closed this as completed Oct 4, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants