Typescript: Typescript 3.9 CommonJS code emitted sets exports to void 0 (leading to error TS2323: Cannot redeclare exported variable)

Created on 13 May 2020  路  3Comments  路  Source: microsoft/TypeScript

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 = '';

https://www.typescriptlang.org/play/?module=1&ts=3.8.3#code/KYDwDg9gTgLgBAYwgOwM71JWBGOBeOAckIG4AoTaeJNDcKgJnyNLKA

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.

Bug Fix Available checkJs

Most helpful comment

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.

All 3 comments

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

https://github.com/telegraf/telegraf/issues/1045

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.

Was this page helpful?
0 / 5 - 0 ratings