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

5.5 Beta - Error assigning const string to enum from another file #58424

Closed
lukpsaxo opened this issue May 3, 2024 · 3 comments
Closed

5.5 Beta - Error assigning const string to enum from another file #58424

lukpsaxo opened this issue May 3, 2024 · 3 comments

Comments

@lukpsaxo
Copy link

lukpsaxo commented May 3, 2024

πŸ”Ž Search Terms

error 5.5 beta "has a string type, but must have syntactically recognizable string syntax when 'isolatedModules' is enabled"

πŸ•— Version & Regression Information

  • This changed between versions 5.4 and 5.5 beta

⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.5.0-beta&ssl=5&ssc=3&pln=1&pc=1#code/KYDwDg9gTgLgBAYwgOwM7wXAvHARAQVwG4AoE4ZAVwFs4AhOAbxLlbgENtEAaEgXyJA

πŸ’» Code

The playground doesn't reproduce - I guess it only happens when importing from another file

otherFile

export const c = "A";
import * as otherFile from './otherFile';

enum B {
    a = otherFile.c,
};

πŸ™ Actual behavior

'otherFile.c' has a string type, but must have syntactically recognizable string syntax when 'isolatedModules' is enabled

πŸ™‚ Expected behavior

No error like in 5.4 - or a better explanation of what I am doing wrong :/

Additional information about the issue

This seems to have also come up in the new errors report:

#57922

@jakebailey
Copy link
Member

jakebailey commented May 3, 2024

Isolated modules means that an emitter only needs to see a single file to know how to emit it. In this case, the error is correct because the type of a impacts whether or not the enum has a reverse mapping or not, and is therefore disallowed as only tsc could know that via analyzing other files (while say, babel, could not).

See also #56153

@lukpsaxo
Copy link
Author

lukpsaxo commented May 3, 2024

Reading the linked issue, I think I get it now.. It just wasn't obvious from the error message - "but must have syntactically recognizable string syntax" - I was thinking somehow the type of the constant was ending up string instead of a specific string e.g. "A"

@lukpsaxo lukpsaxo closed this as completed May 3, 2024
@fatcerberus
Copy link

"Must have syntactically-recognizable string syntax" or in other words "we must be able to tell that this is a string (and not, say, a number) just from looking at the syntax", which is not the case here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants