TypeScript Version: 3.9.2
Search Terms:
TS2323: Cannot redeclare exported variable, CommonJS, exports
Code
export const export1 = '';
export const export2 = '';
Expected behavior:
in TypeScript 3.8.x, with --module CommonJS
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.export1 = '';
exports.export2 = '';
Actual behavior:
in TypeScript 3.9.2, with --module CommonJS
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.export2 = exports.export1 = void 0;
exports.export1 = '';
exports.export2 = '';
Playground Link:
https://www.typescriptlang.org/play/?module=1&ts=3.9.2#code/KYDwDg9gTgLgBAYwgOwM71JWBGOBeOAckIG4AoTaeJNDcKgJnyNLKA
This is a regression because the code emitted leads to compiler errors when imported by javascript when --checkJs is true
error TS2323: Cannot redeclare exported variable 'export1'
error TS2323: Cannot redeclare exported variable 'export2'
I've worked around this by setting --declaration to true and excluding the javascript, but there doesn't seem to be a reason why this change was made.
TS won't always be able to generate code that checks cleanly under checkJs; you should not treat this as an invariant.
The extra assignment is there for spec compliance purposes; during the initializer of export1, export2 needs to be visible as a member of the containing module, even though its initializer hasn't run yet.
In JS mode, we should probably ignore explicit initializtions to void0.
I have the same error with TS 3.9.3 using telegraf library. Forced to downgrade to TS 3.8.3.
Object.defineProperty(o, "default", { enumerable: true, value: v });
^
TypeError: Cannot redefine property: default
After dividing import into two lines, from:
import Telegraf, { Context, Markup, Extra } from "telegraf";
to:
import Telegraf from "telegraf";
import { Context, Markup, Extra } from "telegraf";
error disappears and all work fine.
Most helpful comment
After dividing import into two lines, from:
to:
error disappears and all work fine.