error TS2539: Cannot assign to 'c' because it is not a variable
See, there's a confusion here. And Dr. Axel Rauschmayer nailed it in this article:
CommonJS modules export values. ES6 modules export bindings - live connections to values.
//------ lib.js ------
export let mutableValue = 3;
export function incMutableValue() {
mutableValue++;
}
//------ main1.js ------
import { mutableValue, incMutableValue } from './lib';
// The imported value is live
console.log(mutableValue); // 3
incMutableValue();
console.log(mutableValue); // 4
// The imported value can’t be changed
mutableValue++; // TypeError
So you have two options:
- adjust the compilerOptions so that your module is treated as CommonJS one
- treat the imported values as bindings (aliases), not true identifiers
Use objects as namespaces:
export let state = {
c : 10 as number;
}
place it inside a class, and make it static
export class GlobalVars {
public static c: any = 10;
}
after importing it from any other file
GlobalVars.c = 100;