質問

I have a typescript module that is split across two files, and tsc (version 0.9.1.1) compiled it without errors, and the produced code ran correctly.

Here is a summary of the source file for my module:
split_module_example.ts:

//file: orders.part1.ts
module Orders {  // first part of Orders
    export var msg : string;
    // set orders here
    msg = 'hello';
}

//file: orders.part1.ts
module Orders {  // second part of Orders
    export var msg : string;
    // reference orders here
    console.log('orders=' + msg);
}

the compiler generates this javascript, which looks correct to me:

//file: orders.part1.ts
var Orders;
(function (Orders) {
    Orders.msg;
    Orders.msg = 'hello';
})(Orders || (Orders = {}));

//file: orders.part1.ts
var Orders;
(function (Orders) {
    Orders.msg;

    // reference orders here
    console.log('orders=' + Orders.msg);
})(Orders || (Orders = {}));

I just upgraded my TypeScript compiler from 0.9.1.1 to 0.9.5.0.

But now when I run tsc:

tsc --out split_module_example.js split_module_example.ts

it issues this error:

split_module_example.ts(9,16): error TS2000: Duplicate identifier 'msg'.

However, the code still runs correctly.

I wonder if I'm doing something wrong, or if there was a bug introduced in the 0.9.5.0 compiler.

  • If I'm doing something wrong, what is the correct way to reference a module internal variable across module fragments?
  • If the compiler is wrong, is there a workaround?
役に立ちましたか?

解決

You should only have the variable declared in exactly one location. Just remove the var declaration from all but one of the modules and everything will work as expected.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top