Skip to content

Bug in helper function that is emitted by the compiler for module re-exports #10476

Closed
@gilboa23

Description

@gilboa23

TypeScript Version: 1.8.10

Code

An AMD module with re-exports:

export * from "a/b";
export * from "a/c";

Generates the following code:

define(["require", "exports", "a/b", "a/c"], function (require, exports, b_1, c_1) {
   "use strict";
   function __export(m) {
      for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
   }
   __export(b_1);
   __export(c_1);
});

Problem:
The emitted helper '__export' function will fail if the exports object has an empty prototype (due to custom module loading system) and thus does not inherit the 'hasOwnProperty' method. It will also not handle correctly exported readonly properties (created by custom decorators).

Workaround:

As a workaround, i currently override the emitted function in my TypeScript code like so:

function __export(m: any) {
   Object.getOwnPropertyNames(m).forEach(p => Object.defineProperty(exports, p, Object.getOwnPropertyDescriptor(m, p)));
}
export * from "a/b";
export * from "a/c";

Please fix the emitted '__exports' function to code similar to the above workaround (at least for ES5/ES6 targets).

Metadata

Metadata

Assignees

No one assigned

    Labels

    Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featureSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions