Skip to content

Create Compatibility with Object.fromEntries() with ES5 #32803

@waynevanson

Description

@waynevanson

Search Terms

Object.fromEntries()

Suggestion

I'd like to see Object.fromEntries() available to use as a function when compiling to the browser compatible ES5.

Use Cases

Using Object.fromEntries() when targeting ES5 alongside the friendly Object.entries()

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/fromEntries

Examples

Here is the polyfill from TC39 https://github.com/tc39/proposal-object-from-entries/blob/master/polyfill.js

  
function ObjectFromEntries(iter) {
  const obj = {};

  for (const pair of iter) {
    if (Object(pair) !== pair) {
      throw new TypeError('iterable for fromEntries should yield objects');
    }

    // Consistency with Map: contract is that entry has "0" and "1" keys, not
    // that it is an array or iterable.

    const { '0': key, '1': val } = pair;

    Object.defineProperty(obj, key, {
      configurable: true,
      enumerable: true,
      writable: true,
      value: val,
    });
  }

  return obj;
}

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

My Questions

  • Is this feature left out intentionally?
  • A guide/where to look when contributing to adding a feature like this?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions