-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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 to use a const in the initializer of a const enum #18887
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 @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.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, I guess so.
src/compiler/checker.ts
Outdated
@@ -21898,6 +21908,13 @@ namespace ts { | |||
} | |||
return undefined; | |||
} | |||
|
|||
function getFromLiteralType(expr: Identifier | ElementAccessExpression | PropertyAccessExpression): string | number | undefined { |
There was a problem hiding this comment.
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.
case SyntaxKind.ElementAccessExpression: | ||
case SyntaxKind.PropertyAccessExpression: | ||
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.
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?
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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.
This only works for constants with literal types? Otherwise there's no way the value can be inlined. |
@ahejlsberg Good to go? |
@ahejlsberg bump |
@andy-ms can you talk to @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.