-
Notifications
You must be signed in to change notification settings - Fork 13k
isolatedModules errors for non-literal enum initializers #56736
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
Changes from 3 commits
c51645a
93b0b89
af97344
6325c1d
e112f4d
dd647aa
baacbe8
42eb9b1
dafdaec
bd28afa
8b926c9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -7936,5 +7936,13 @@ | |||||
"'await using' statements cannot be used inside a class static block.": { | ||||||
"category": "Error", | ||||||
"code": 18054 | ||||||
}, | ||||||
"A string member initializer in a enum declaration can only use constant expressions when 'isolatedModules' is enabled.": { | ||||||
|
"A string member initializer in a enum declaration can only use constant expressions when 'isolatedModules' is enabled.": { | |
"'{0}' has a string type, but must have syntactically recognizable string syntax when 'isolatedModules' is enabled.": { |
We could totally do a quick fix to wrap it in a template literal but I think this error is going to be so niche that I’m not sure it’s worth it.
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.
I like the suggested error message. But what do I substitute {0}
with? The initializer can be a relatively complex expression. Is there a utility function to stringify that? Or should I use the enum member name or even the computed value?
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.
I forgot to include that; Daniel’s suggestion was 'EnumName.MemberName'
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.
Sounds good. Do we want to handle member names that are not valid identifier? E.g.:
import {bar} from './bar';
enum Foo { ['not an identifier'] = bar }
// 'Foo.not an identifier' has a string type, ...
// 'Foo["not an identifier"]' has a string type, ...
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.
I updated the error message and ignored the non-identifier case for now, because it seems like an extremely rare case. That means it would print 'Foo.not an identifier'
in the error message.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
bad.ts(4,5): error TS18056: Enum member following a non-literal numeric member must have an initializer when 'isolatedModules' is enabled. | ||
|
||
|
||
==== ./helpers.ts (0 errors) ==== | ||
export const foo = 2; | ||
|
||
==== ./bad.ts (1 errors) ==== | ||
import { foo } from "./helpers"; | ||
enum A { | ||
a = foo, | ||
b, | ||
~ | ||
!!! error TS18056: Enum member following a non-literal numeric member must have an initializer when 'isolatedModules' is enabled. | ||
} | ||
|
||
==== ./good.ts (0 errors) ==== | ||
import { foo } from "./helpers"; | ||
enum A { | ||
a = foo, | ||
b = 3, | ||
} | ||
enum B { | ||
a = 1 + 1, | ||
b, | ||
} | ||
enum C { | ||
a = +2, | ||
b, | ||
} | ||
enum D { | ||
a = (2), | ||
b, | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
bad.ts(3,8): error TS18055: A string member initializer in a enum declaration can only use constant expressions when 'isolatedModules' is enabled. | ||
|
||
|
||
==== ./helpers.ts (0 errors) ==== | ||
export const foo = 2; | ||
|
||
==== ./bad.ts (1 errors) ==== | ||
import { foo } from "./helpers"; | ||
enum A { | ||
a = `${foo}` | ||
~~~~~~~~ | ||
!!! error TS18055: A string member initializer in a enum declaration can only use constant expressions when 'isolatedModules' is enabled. | ||
} | ||
|
||
==== ./good.ts (0 errors) ==== | ||
enum A { | ||
a = `${"foo"}`, | ||
b = "" + 2, | ||
c = 2 + "", | ||
d = ("foo"), | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// @isolatedModules: true | ||
// @noEmit: true | ||
frigus02 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
// @noTypesAndSymbols: true | ||
|
||
// @filename: ./helpers.ts | ||
export const foo = 2; | ||
|
||
// @filename: ./bad.ts | ||
import { foo } from "./helpers"; | ||
enum A { | ||
a = foo, | ||
b, | ||
} | ||
|
||
// @filename: ./good.ts | ||
import { foo } from "./helpers"; | ||
enum A { | ||
a = foo, | ||
b = 3, | ||
} | ||
enum B { | ||
a = 1 + 1, | ||
b, | ||
} | ||
enum C { | ||
a = +2, | ||
b, | ||
} | ||
enum D { | ||
a = (2), | ||
b, | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// @isolatedModules: true | ||
// @noEmit: true | ||
frigus02 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
// @noTypesAndSymbols: true | ||
|
||
// @filename: ./helpers.ts | ||
export const foo = 2; | ||
|
||
// @filename: ./bad.ts | ||
import { foo } from "./helpers"; | ||
enum A { | ||
a = `${foo}` | ||
} | ||
|
||
// @filename: ./good.ts | ||
enum A { | ||
a = `${"foo"}`, | ||
b = "" + 2, | ||
c = 2 + "", | ||
d = ("foo"), | ||
} |
Uh oh!
There was an error while loading. Please reload this page.