You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Certain resolution-dependent enum emit isn't correctly flagged as an error under isolatedModules #56153
Emit a warning or an error, and generate code that does not rely on other imports.
Just to be clear, isolatedModulesdoesn't mean "never emit code that relies on an import", it means "correct emit doesn't require resolving imports". This should probably still be an error under that reasoning though, since the emit requires the actual value of Foo.A.
isolatedModules means that a non-resolving transpiler can correctly emit any file in the project. Arguably, a non-resolving transpiler can correctly emit index.ts:
C is an error, so its correct emit is not guaranteed. Correct transpilation (in the presence of no errors) a motivating scenario behind the breaking changes we made in #50528
C is an error, so its correct emit is not guaranteed. Correct transpilation (in the presence of no errors) a motivating scenario behind the breaking changes we made in #50528
You are right.
If people always handle errors and do not expect consistent results from erroneous inputs, then things would be much simpler.
However, as far as I know, there are still people who try to use any to bypass error checking, but expect to get the same results as the TypeScript compiler.
I would think that the moment you're doing something unsafe like casting an enum or ts-ignoreing, all bets are off about actual runtime behavior. The only "safe" thing I think exists is to assume one can use a const enum as a value unsafely if preserveConstEnums is enabled (esbuild will keep them around), but that idea only works for local enums.
changed the title [-]`isolatedModules: true` still link enum cross module[/-][+]Certain resolution-dependent enum emit isn't correctly flagged as an error under `isolatedModules`[/+]on Oct 20, 2023
👋 Hi, I'm the Repro bot. I can help narrow down and track compiler bugs across releases! This comment reflects the current state of the repro in the issue body running against the nightly TypeScript.
👋 Hi, I'm the Repro bot. I can help narrow down and track compiler bugs across releases! This comment reflects the current state of this repro running against the nightly TypeScript.
When an enum initializer expression that isn't a string literal has a string type
Could we allow template expressions? E.g. the following is guaranteed to have a string value. How do you feel about allowing that and not generating a reverse mapping for it?
enumFoo{A=`${Bar.A}`}
Edit: Never mind. Turns out in our code base this pattern happens so rarely, this doesn't feel worth the effort.
Activity
fatcerberus commentedon Oct 19, 2023
Just to be clear,
isolatedModulesdoesn't mean "never emit code that relies on an import", it means "correct emit doesn't require resolving imports". This should probably still be an error under that reasoning though, since the emit requires the actual value ofFoo.A.RyanCavanaugh commentedon Oct 19, 2023
isolatedModulesmeans that a non-resolving transpiler can correctly emit any file in the project. Arguably, a non-resolving transpiler can correctly emitindex.ts:So the bug here is either that:
ts.transpileModuleisn't emitting the addition expressionCshould be an errorI'm leaning toward the first option for the sake of compat and convenience.
magic-akari commentedon Oct 19, 2023
I don't believe
Foo.A + 1is correct. You can do this only if you knowFoo.Ais a number.In the above example, tsc will return
void 0forBar.C, whileFoo.A + 1will produce the string"101"In this example,
Foo.A + 1gets 11, while tsc returns the value ofvoid 0.RyanCavanaugh commentedon Oct 19, 2023
Cis an error, so its correct emit is not guaranteed. Correct transpilation (in the presence of no errors) a motivating scenario behind the breaking changes we made in #50528magic-akari commentedon Oct 19, 2023
You are right.
If people always handle errors and do not expect consistent results from erroneous inputs, then things would be much simpler.
However, as far as I know, there are still people who try to use
anyto bypass error checking, but expect to get the same results as the TypeScript compiler.One possible input:
magic-akari commentedon Oct 19, 2023
CC Nicolò Ribaudo (@nicolo-ribaudo) Evan Wallace (@evanw)
Maybe same issue in
Babeland esbuild (transform mode)jakebailey commentedon Oct 19, 2023
I would think that the moment you're doing something unsafe like casting an enum or
ts-ignoreing, all bets are off about actual runtime behavior. The only "safe" thing I think exists is to assume one can use aconst enumas a value unsafely ifpreserveConstEnumsis enabled (esbuild will keep them around), but that idea only works for local enums.magic-akari commentedon Oct 20, 2023
Ryan Cavanaugh (@RyanCavanaugh) Sorry to bother you again.
I am trying to implement this, but it does not work.
Workbench Repro
The tsc says that the
Bar.Cis 2.But I got
A1throughBar.B + 1,or
1Athrough1 + Bar.B.Babel playground cc Nicolò Ribaudo (@nicolo-ribaudo)
nicolo-ribaudo commentedon Oct 20, 2023
That's a separate bug -- TSC should probably disallow
"Infinity"and"NaN"as keys the same way as it disallows"3".magic-akari commentedon Oct 20, 2023
I am trying to fix this. But I found that
"Infinity""-Infinity"and"NaN"were intentionally excluded.https://github.com/microsoft/TypeScript/blob/6424e181d4d7460913e730249a078b176fc8aaf0/src/compiler/checker.ts#L44730-L44735
nicolo-ribaudo commentedon Oct 20, 2023
It was accepted as a bug at #48956.
RyanCavanaugh commentedon Oct 20, 2023
TL;DR from #56164.
Under
isolatedModulesonly, the following should be errors:[-]`isolatedModules: true` still link enum cross module[/-][+]Certain resolution-dependent enum emit isn't correctly flagged as an error under `isolatedModules`[/+]nicolo-ribaudo commentedon Oct 20, 2023
(from the meeting notes)
Note that Babel currently transforms
to
Is this what was meant by "work"? Or is this correct only under the new proposed behavior?
RyanCavanaugh commentedon Oct 20, 2023
Yes, though under the agreed-on rules,
Cwould be an errortypescript-bot commentedon Oct 21, 2023
👋 Hi, I'm the Repro bot. I can help narrow down and track compiler bugs across releases! This comment reflects the current state of the repro in the issue body running against the nightly TypeScript.
Issue body code block by magic-akari
👍 Compiled
Emit:
Historical Information
👍 Compiled
Emit:
typescript-bot commentedon Oct 21, 2023
👋 Hi, I'm the Repro bot. I can help narrow down and track compiler bugs across releases! This comment reflects the current state of this repro running against the nightly TypeScript.
Comment by magic-akari
👍 Compiled
Emit:
Historical Information
👍 Compiled
Emit:
frigus02 commentedon Dec 11, 2023
Could we allow template expressions? E.g. the following is guaranteed to have a string value. How do you feel about allowing that and not generating a reverse mapping for it?
Edit: Never mind. Turns out in our code base this pattern happens so rarely, this doesn't feel worth the effort.