I've been trying to test out a module that simply exports a number, and a function that increments that number:
// counter.js
export var number = 0;
export function increment() {
number++;
}
I'm then using this module as follows:
// index.js
import { number, increment } from './counter';
console.log(number);
increment();
console.log(number);
I'm using JSPM for the module loading, in case that's important. If I run this I get my expected output:
0
1
However, if I simply change counter.js to counter.ts and have TypeScript transpile this module, my output ends up being:
0
0
The transpiled counter.js looks like:
// counter.js
"use strict";
exports.number = 0;
function increment() {
exports.number++;
}
exports.increment = increment;
Also, here is my tsconfig.json:
// tsconfig.json
{
"compilerOptions": {
"module": "commonjs",
"target": "es5",
"noImplicitAny": false,
"sourceMap": false
}
}
Is this a bug in how TypeScript is transpiling this module (and exporting a constant value for number, rather than the variable itself) or am I missing something here?
I have the exact same transpiled output and tsconfig.json settings, but I get the expected output:
0
1
The only difference I'm seeing is that you're using JSPM here, whereas I am not; I'm just running your code with Node.JS (v6.2.0, with TypeScript 3.8.9).
If it helps, the tsconfig.json documentation for valid --module options says amd, commonjs, system
are some valid options.
I created a git repo of the working code.
https://github.com/TWebster/counter
I'm debugging with vscode in case it matters.
Updating my tsconfig.json with the following gave me my expected output:
{
"compilerOptions": {
"module": "system"
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.