Skip to content

Unable to compile Object.fromEntries -- polyfill required #968

Description

@abhimanyuPathania

Current Behavior

I am building an NPM package for Node using the 'basic' template. I tried using Object.fromEntries in my code but get the error:

TypeError: Object.fromEntries is not a function

Upon inspecting the .cjs.development.js output it leaves Object.fromEntries untransformed which I guess are not supported in Node 10. I tried using build --target node which does not resolve it. Changing target on tsconfig also does not fix this.

Expected behavior

Typescript or Babel should be able to compile Object.fromEntries

Suggested solution(s)

Maybe a Babel plugin or polyfill is missing.

Additional context

Following is code snippet and transformed output
Code:

export default class Platform {
  id: string;
  name: string;
  channelLabelVsIdMap: Record<string, number>;
  features: Record<string, Feature | undefined>;

  constructor(platform: TPlatformMetadata, metadata: TMetadataResponse) {
    const { features = [] } = metadata;
    this.id = platform.id;
    this.name = platform.name;
    this.channelLabelVsIdMap = platform.channelLabelVsIdMap;

    this.features = Object.fromEntries(
      features
        .filter((feature) => feature.platformId === platform.id)
        .map((feature) => [feature.id, new Feature(feature, metadata)]),
    );
  }
}

Transformed output tsdx build

var Platform = function Platform(platform, metadata) {
  var _metadata$features = metadata.features,
      features = _metadata$features === void 0 ? [] : _metadata$features;
  this.id = platform.id;
  this.name = platform.name;
  this.channelLabelVsIdMap = platform.channelLabelVsIdMap;
  this.features = Object.fromEntries(features.filter(function (feature) {
    return feature.platformId === platform.id;
  }).map(function (feature) {
    return [feature.id, new Feature(feature, metadata)];
  }));
};

Your environment

  System:
    OS: macOS 10.15.3
    CPU: (8) x64 Intel(R) Core(TM) i7-4770HQ CPU @ 2.20GHz
    Memory: 792.30 MB / 16.00 GB
    Shell: 5.7.1 - /bin/zsh
  Binaries:
    Node: 10.22.0 - ~/.nvm/versions/node/v10.22.0/bin/node
    Yarn: 1.22.4 - /usr/local/bin/yarn
    npm: 6.14.6 - ~/.nvm/versions/node/v10.22.0/bin/npm
  Browsers:
    Chrome: 88.0.4324.96
    Firefox: 84.0.2
    Safari: 13.0.5
  npmPackages:
    tsdx: ^0.14.1 => 0.14.1
    typescript: ^4.1.3 => 4.1.3

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    kind: supportAsking for support with something or a specific use casesolution: intended behaviorThis is not a bug and is expected behaviorsolution: out-of-scopeThis is out of scope for this project

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions