admin管理员组文章数量:1426907
I am trying to re-export a variable using es6 module syntax, then change it and see the change reflected in the final import. But it is not working as expected. See the example below:
a.ts
export var a = 1;
export function changeA() { a = 2; }
b.ts
export * from './a';
c.ts
import { a, changeA } from './b';
console.log(a); // 1
changeA();
console.log(a); // Expected 2 but get 1
tsconfig.json
{
"pilerOptions": {
"target": "es5",
"module": "monjs",
"outDir": "out"
}
}
Result of run:
$ node out/c.js
1
1
I expect the final console.log to print 2 in order to reflect the update but it does not. However, if I pile the same example with babel it works. Does re-exporting mutable variable bindings not work with typescript at all or am I just doing something wrong?
I am trying to re-export a variable using es6 module syntax, then change it and see the change reflected in the final import. But it is not working as expected. See the example below:
a.ts
export var a = 1;
export function changeA() { a = 2; }
b.ts
export * from './a';
c.ts
import { a, changeA } from './b';
console.log(a); // 1
changeA();
console.log(a); // Expected 2 but get 1
tsconfig.json
{
"pilerOptions": {
"target": "es5",
"module": "monjs",
"outDir": "out"
}
}
Result of run:
$ node out/c.js
1
1
I expect the final console.log to print 2 in order to reflect the update but it does not. However, if I pile the same example with babel it works. Does re-exporting mutable variable bindings not work with typescript at all or am I just doing something wrong?
Share Improve this question edited Jan 5, 2016 at 13:11 Venkat.R 7,7765 gold badges44 silver badges68 bronze badges asked Jan 5, 2016 at 12:00 Jonas KelloJonas Kello 1,4323 gold badges16 silver badges29 bronze badges1 Answer
Reset to default 6It is because b.ts
:
export * from './a';
transpiles to
function __export(m) {
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
}
__export(require('./a'));
and the value of variable a
is copied and not referenced.
You can work it around this way:
a.ts:
export var a = 1;
export var deeper = {
a: 1
};
export function changeA() {
a = 2;
deeper.a = 2;
}
b.ts:
export * from './a';
c.ts:
import { a, deeper, changeA } from './b';
console.log(a); // 1
changeA();
console.log(a); // Expected 2 but get 1
console.log(deeper.a); // prints 2 as expected
本文标签: javascriptTypescript es6 modules reexport mutable variable bindingStack Overflow
版权声明:本文标题:javascript - Typescript es6 modules re-export mutable variable binding - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745487568a2660451.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论