I have 2 .ts files,
C.ts:
export let c: any = 10;
A.ts:
import { c } from "./C";
c = 100;
When I compile A.ts, a error:
error TS2539: Cannot assign to 'c' because it is not a variable.
How can I fix it?
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;
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:
Use objects as namespaces:
export let state = {
c : 10 as number;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With