Repository navigation
Allow to use a const in the initializer of a const enum - #18887
Deleted user (ghost) wants to merge 7 commits into
Conversation
|
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 |
|
From discussion with Ryan Cavanaugh (@RyanCavanaugh): It looks like |
| @@ -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. | |||
There was a problem hiding this comment.
it would be nice to have this error go away and see only the new one.
There was a problem hiding this comment.
Is it good enough to have the new error message?
There was a problem hiding this comment.
Sure, I guess so.
| return undefined; | ||
| } | ||
|
|
||
| function getFromLiteralType(expr: Identifier | ElementAccessExpression | PropertyAccessExpression): string | number | undefined { |
There was a problem hiding this comment.
getTypeFromLiteral[Type]Node? The current name doesn't mean anything to me at all.
| const ex = <PropertyAccessExpression | ElementAccessExpression>expr; | ||
| if (isConstantMemberAccess(ex)) { | ||
| const type = getTypeOfExpression(ex.expression); | ||
| const type = checkExpression(ex.expression); |
There was a problem hiding this comment.
why can't we use getTypeOfExpression anymore?
There was a problem hiding this comment.
We can, but it just has special handling for CallExpression that we don't need, then calls checkExpression.
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
could do with a better error message here too; x is const, but circular.
|
This only works for constants with literal types? Otherwise there's no way the value can be inlined. |
|
Anders Hejlsberg (@ahejlsberg) Good to go? |
|
Andy (Andrewkraft) (@Andy-MS) can you talk to Anders Hejlsberg (@ahejlsberg) about this change |
|
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. |
Was there any reason we didn't do this already? It doesn't seem to cause errors in the presence of circularity.