Skip to content

Allow to use a const in the initializer of a const enum - #18887

Closed
Deleted user (ghost) wants to merge 7 commits into
masterfrom
enumFoo
Closed

Deleted user (ghost) wants to merge 7 commits into
masterfrom
enumFoo

Conversation

@ghost

Copy link
Copy Markdown

Was there any reason we didn't do this already? It doesn't seem to cause errors in the presence of circularity.

@RyanCavanaugh

Ryan Cavanaugh (RyanCavanaugh) commented Oct 2, 2017 •

Copy link
Copy Markdown
Member

file1.ts

import { b } from './file2';

export const enum X {
  a = 1,
  c = b
}

file2.ts

export const b = 10;

In your branch this compiles cleanly under --isolatedModules but file1.ts will generate different (and possibly non-working) code under transpileModule

@ghost

Deleted user (ghost) commented Oct 3, 2017 •

Copy link
Copy Markdown
Author

From discussion with Ryan Cavanaugh (@RyanCavanaugh): It looks like --isolatedModules just ignores the const in const enum anyway and produces accesses like X.c everywhere, so this won't affect that.
Ping Anders Hejlsberg (@ahejlsberg) to see if there are any other reasons we don't support this.

@@ -36,9 +38,13 @@ tests/cases/compiler/constEnumErrors.ts(42,9): error TS2478: 'const' enum member
Y = E1.Z,
~~~~
!!! error TS2474: In 'const' enum declarations member initializer must be constant expression.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it would be nice to have this error go away and see only the new one.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it good enough to have the new error message?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I guess so.

Comment thread src/compiler/checker.ts Outdated
return undefined;
}

function getFromLiteralType(expr: Identifier | ElementAccessExpression | PropertyAccessExpression): string | number | undefined {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getTypeFromLiteral[Type]Node? The current name doesn't mean anything to me at all.

Comment thread src/compiler/checker.ts
const ex = <PropertyAccessExpression | ElementAccessExpression>expr;
if (isConstantMemberAccess(ex)) {
const type = getTypeOfExpression(ex.expression);
const type = checkExpression(ex.expression);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why can't we use getTypeOfExpression anymore?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can, but it just has special handling for CallExpression that we don't need, then calls checkExpression.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, that makes sense. I thought getTypeOfExpression was the fast path that was called by checkExpression, but I guess it actually just has some initial fast-path checks before delegating to checkExpression.

const enum E {
y = x,
~
!!! error TS2474: In 'const' enum declarations member initializer must be constant expression.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could do with a better error message here too; x is const, but circular.

@ajafff

Copy link
Copy Markdown
Contributor

This only works for constants with literal types? Otherwise there's no way the value can be inlined.
There should probably be a test for non-literal types, e.g. : string or : 1 | 2

@ghost

Copy link
Copy Markdown
Author
@ghost

Copy link
Copy Markdown
Author
@ghost

Copy link
Copy Markdown
Author
@mhegazy

Copy link
Copy Markdown
Contributor
@typescript-bot

Copy link
Copy Markdown
Contributor

Thanks for your contribution. This PR has not been updated in a while and cannot be automatically merged at the time being. For housekeeping purposes we are closing stale PRs. If you'd still like to continue working on this PR, please leave a message and one of the maintainers can reopen it.

@ghost
ghost deleted the enumFoo branch June 13, 2018 14:22
@microsoft Microsoft (microsoft) locked as resolved and limited conversation to collaborators Oct 21, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

6 participants