X Tutup
The Wayback Machine - https://web.archive.org/web/20201207172844/https://github.com/microsoft/TypeScript/issues/39751
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

Error message for assigning to an imported variable is confusing #39751

Open
uhyo opened this issue Jul 26, 2020 · 6 comments · May be fixed by #39827
Open

Error message for assigning to an imported variable is confusing #39751

uhyo opened this issue Jul 26, 2020 · 6 comments · May be fixed by #39827

Comments

@uhyo
Copy link
Contributor

@uhyo uhyo commented Jul 26, 2020

Search Terms

TS2539 import variable

Suggestion

import { v } from "somewhere";
// Error TS2539: Cannot assign to 'v' because it is not a variable.
v = 100;

The error message for TS2539 is confusing because v is actually a variable (at least regarding the ES spec where imported ones and other normal variables are described as different kinds of "bindings") . The true reason of the error is that it is an imported variable, so it would be better if we changed the message to, for example:

  • Cannot assign to v because it is an imported variable.
  • Cannot assign to v because it is immutable.
  • Cannot assign to v because it is a constant. (same as const variable)

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.
@uhyo uhyo changed the title Error message for TS2539 is confusing Error message for assigning to an imported variable is confusing Jul 26, 2020
@DanielRosenwasser
Copy link
Member

@DanielRosenwasser DanielRosenwasser commented Jul 29, 2020

There's a slight subtlety here which is that there's another syntax called "namespace import aliases".

   namespace foo { export var x = 100; }
   
   import X = foo;
   
   X = { x: 100 };
// ~
// Cannot assign to 'X' because it is not a variable.

Maybe it's fine to issue the same message on these too.

@hailin-zhang hailin-zhang linked a pull request that will close this issue Jul 30, 2020
@hailin-zhang
Copy link

@hailin-zhang hailin-zhang commented Jul 30, 2020

Hello, I've created a PR for this issue. There were a couple different cases for this error so I've opted to change it to Cannot assign to {{x}} because it is immutable. Please let me know if a more descriptive message is preferred 😄

@DanielRosenwasser
Copy link
Member

@DanielRosenwasser DanielRosenwasser commented Jul 30, 2020

I don't think "immutable" is right either, but I'll try to provide something better in the PR - thanks!

@mohamadhamad300
Copy link

@mohamadhamad300 mohamadhamad300 commented Aug 8, 2020

hello
adjust the compilerOptions so that your module is treated as CommonJS one
treat the imported values as bindings (aliases), not true identifier

@kylejlin
Copy link

@kylejlin kylejlin commented Oct 3, 2020

Hi everyone! I'd be happy to create a PR for this issue, but I'm not certain what the new error message should be.

How about Cannot assign to {{x}} because it is an import binding.? Let me know if you have suggestions for alternatives.

@kylejlin
Copy link

@kylejlin kylejlin commented Oct 3, 2020

Oh, wait. I just realized that this is the same error message that gets used when the user tries to reassign other non-variables (such as functions or enums). I guess these were the cases @hailin-zhang was referring to.

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

Successfully merging a pull request may close this issue.

6 participants
You can’t perform that action at this time.
X Tutup