Home > Net >  Assign value to imported variable not possible. Why?
Assign value to imported variable not possible. Why?

Time:12-22

In Typescript I get the following error : Cannot assign to variable 'x' because it is an import.

My Variable x is in another file and I imported it in some different file.

And if I try to assign no value it does not work.

If I have an outsourced interface it works though.

export const environment = {

  x: undefined

}

Now I can import it again and do enviroment.x = 10 ;

So what's different ??

CodePudding user response:

You have already assign undefined value to environment.x, for which typescript implied environment.x has type undefined.

You cannot assign a number type to an undefined type.

You could assign environment.x with a number type and change it later though.

For example:

export const environment = {
  x: -1
}

CodePudding user response:

I think you can use the import = syntax to import the value as a mutable variable. For example:

import = x from "./someModule";

x = 10; // should work

Not sure if this is what you want or something different.

  • Related