Skip to content
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

Certain resolution-dependent enum emit isn't correctly flagged as an error under isolatedModules #56153

Closed
magic-akari opened this issue Oct 19, 2023 · 17 comments · Fixed by #56736
Labels
Bug A bug in TypeScript Has Repro This issue has compiler-backed repros: https://aka.ms/ts-repros Help Wanted You can do this
Milestone

Comments

@magic-akari
Copy link
Contributor

magic-akari commented Oct 19, 2023

🔎 Search Terms

  • isolatedModules
  • enum
  • cross module
  • export

🕗 Version & Regression Information

  • This is a crash
  • This changed between versions ______ and _______
  • This changed in commit or PR _______
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about _________
  • I was unable to test this on prior versions because _______

Test on Playground with version 3.9.7 and version 5.2.2, got the same incorrect result.

⏯ Playground Link

Workbench Repro

💻 Code

// @showEmit
// @isolatedModules: true
// @filename: foo.ts
export enum Foo {
    A = 10
}

// @filename: index.ts
import { Foo } from "./foo";
export enum Bar {
    B = Foo.A,
    C
}

Workbench Repro

🙁 Actual behavior

Please notice the value of Bar.C

import { Foo } from "./foo";
export var Bar;
(function (Bar) {
    Bar[Bar["B"] = 10] = "B";
    Bar[Bar["C"] = 11] = "C";
})(Bar || (Bar = {}));

🙂 Expected behavior

Emit a warning or an error, and generate code that does not rely on other imports.

Example:

import { Foo } from "./foo";
export var Bar;
(function(Bar) {
    Bar[Bar["B"] = Foo.A] = "B";
    Bar[Bar["C"] = void 0] = "C";
})(Bar || (Bar = {}));

The Bar.C is void 0, since it cannot be refered.

Additional information about the issue

@fatcerberus
Copy link

Emit a warning or an error, and generate code that does not rely on other imports.

Just to be clear, isolatedModules doesn'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.

@RyanCavanaugh
Copy link
Member

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:

import { Foo } from "./foo";
export var Bar;
(function(Bar) {
    Bar[Bar["B"] = Foo.A] = "B";
    Bar[Bar["C"] = Foo.A + 1] = "C";
})(Bar || (Bar = {}));

So the bug here is either that:

  • ts.transpileModule isn't emitting the addition expression
  • or we say that transpilers shouldn't do the addition, and C should be an error

I'm leaning toward the first option for the sake of compat and convenience.

@magic-akari
Copy link
Contributor Author

I don't believe Foo.A + 1 is correct. You can do this only if you know Foo.A is a number.

// @filename: foo.ts
export enum Foo{
    A = "10"
}

// @filename: index.ts
import { Foo } from "./foo";
export enum Bar {
    B = Foo.A,
    C
}

In the above example, tsc will return void 0 for Bar.C, while Foo.A + 1 will produce the string "101"

// @filename: foo.ts
export var Foo = {
    A: 10
};

// @filename: index.ts
import { Foo } from "./foo";
export enum Bar {
    B = Foo.A,
    C
}

In this example, Foo.A + 1 gets 11, while tsc returns the value of void 0.

@RyanCavanaugh
Copy link
Member

RyanCavanaugh commented Oct 19, 2023

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

@magic-akari
Copy link
Contributor Author

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.

One possible input:

// @filename: foo.ts
export enum Foo {
    A = 10,
}
(Foo.A as any) = "10"

// @filename: index.ts
import { Foo } from "./foo";
export enum Bar {
    B = Foo.A,
    C
}

@magic-akari
Copy link
Contributor Author

magic-akari commented Oct 19, 2023

CC @nicolo-ribaudo @evanw

Maybe same issue in Babel and esbuild (transform mode)

@jakebailey
Copy link
Member

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.

@magic-akari
Copy link
Contributor Author

@RyanCavanaugh Sorry to bother you again.
I am trying to implement this, but it does not work.

// @showEmit
// @isolatedModules: true
// @filename: foo.ts
export enum Foo {
    "Infinity" = 1,
    A = 1 / 0,
}

// @filename: index.ts
import { Foo } from "./foo";
export enum Bar {
    B = Foo.Infinity,
    C
}

Workbench Repro

The tsc says that the Bar.C is 2.
But I got A1 through Bar.B + 1,
or 1A through 1 + Bar.B.

Babel playground cc @nicolo-ribaudo

@nicolo-ribaudo
Copy link

That's a separate bug -- TSC should probably disallow "Infinity" and "NaN" as keys the same way as it disallows "3".

@magic-akari
Copy link
Contributor Author

That's a separate bug -- TSC should probably disallow "Infinity" and "NaN" as keys the same way as it disallows "3".

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
Copy link

It was accepted as a bug at #48956.

@RyanCavanaugh
Copy link
Member

TL;DR from #56164.

Under isolatedModules only, the following should be errors:

  • When an enum initializer expression that isn't a string literal has a string type
  • When an enum member without an initialization expression follows an enum member with an initialization expression that isn't a numeric literal

@RyanCavanaugh RyanCavanaugh added Bug A bug in TypeScript Help Wanted You can do this labels Oct 20, 2023
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Oct 20, 2023
@RyanCavanaugh RyanCavanaugh changed the title isolatedModules: true still link enum cross module Certain resolution-dependent enum emit isn't correctly flagged as an error under isolatedModules Oct 20, 2023
@typescript-bot typescript-bot added the Has Repro This issue has compiler-backed repros: https://aka.ms/ts-repros label Oct 20, 2023
@nicolo-ribaudo
Copy link

nicolo-ribaudo commented Oct 20, 2023

(from the meeting notes)

On the other hand, there is nothing preventing compilers from making these enums "work".

Note that Babel currently transforms

enum Bar {
    B = Foo.A,
    C
}

to

var Bar = function (Bar) {
  Bar[Bar["B"] = Foo.A] = "B";
  Bar[Bar["C"] = 1 + Bar["B"]] = "C";
  return Bar;
}(Bar || {});

Is this what was meant by "work"? Or is this correct only under the new proposed behavior?

@RyanCavanaugh
Copy link
Member

RyanCavanaugh commented Oct 20, 2023

Is this what was meant by "work"

Yes, though under the agreed-on rules, C would be an error

@typescript-bot
Copy link
Collaborator

typescript-bot commented 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:

import { Foo } from "./foo";
export var Bar;
(function (Bar) {
    Bar[Bar["B"] = 10] = "B";
    Bar[Bar["C"] = 11] = "C";
})(Bar || (Bar = {}));

Historical Information
Version Reproduction Outputs
4.8.2, 4.9.3, 5.0.2, 5.1.3, 5.2.2

👍 Compiled
Emit:

import { Foo } from "./foo";
export var Bar;
(function (Bar) {
    Bar[Bar["B"] = 10] = "B";
    Bar[Bar["C"] = 11] = "C";
})(Bar || (Bar = {}));

@typescript-bot
Copy link
Collaborator

typescript-bot commented 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:

import { Foo } from "./foo";
export var Bar;
(function (Bar) {
    Bar[Bar["B"] = 1] = "B";
    Bar[Bar["C"] = 2] = "C";
})(Bar || (Bar = {}));

Historical Information
Version Reproduction Outputs
4.8.2, 4.9.3, 5.0.2, 5.1.3, 5.2.2

👍 Compiled
Emit:

import { Foo } from "./foo";
export var Bar;
(function (Bar) {
    Bar[Bar["B"] = 1] = "B";
    Bar[Bar["C"] = 2] = "C";
})(Bar || (Bar = {}));

@frigus02
Copy link
Contributor

frigus02 commented Dec 11, 2023

  • 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?

enum Foo { A = `${Bar.A}` }

Edit: Never mind. Turns out in our code base this pattern happens so rarely, this doesn't feel worth the effort.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Has Repro This issue has compiler-backed repros: https://aka.ms/ts-repros Help Wanted You can do this
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants