Typescript: Add ES2019 Object.fromEntries function

Created on 15 Apr 2019  路  9Comments  路  Source: microsoft/TypeScript

Search Terms

fromEntries

Suggestion

lib: es2019 should add Object.fromEntries() type.

Use Cases

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

Examples

Checklist

My suggestion meets these guidelines:

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

Most helpful comment

Go into tsconfig.json, and inside compilerOptions properties search for a property named lib. Add the string "esnext" in the array. It should look something like:
"lib": ["es2018", "dom", "esnext"]

All 9 comments

HI Guys,

Is this live yet and if so, which version? I can't get it to find it as Property 'fromEntries' does not exist on type 'ObjectConstructor'.

What versions of TS and NODEJS should I be running to make this work?

The file is available in my node_modules under lib.es2019.object.d.ts but it won't read from it?

Any link to any issue regarding how to resolve a similar issue is welcomed.

/// <reference no-default-lib="true"/>


/// <reference lib="es2015.iterable" />

interface ObjectConstructor {
    /**
     * Returns an object created by key-value entries for properties and methods
     * @param entries An iterable object that contains key-value entries for properties and methods.
     */
    fromEntries<T = any>(entries: Iterable<readonly [PropertyKey, T]>): { [k in PropertyKey]: T };

    /**
     * Returns an object created by key-value entries for properties and methods
     * @param entries An iterable object that contains key-value entries for properties and methods.
     */
    fromEntries(entries: Iterable<readonly any[]>): any;
}

HI Guys,

Is this live yet and if so, which version? I can't get it to find it as Property 'fromEntries' does not exist on type 'ObjectConstructor'.

What versions of TS and NODEJS should I be running to make this work?

It looks to me that in tsconfig.json, the setting to use is target: "es2019" or greater. This means it won't work in all browsers and typescrcipt does no magic to use this.

this is what I did

declare global {
  interface ObjectConstructor {
    fromEntries(xs: [string|number|symbol, any][]): object
  }
}

const fromEntries = (xs: [string|number|symbol, any][]) =>
  Object.fromEntries ? Object.fromEntries(xs) : xs.reduce((acc, [key, value]) => ({...acc, [key]: value}), {})

Extending @bennypowers to sort an object similar to this:

{
  foo: 'bar',
  baz: 'boo',
  alpha: 'beta',
}

use

Object.keys(input).sort().reduce((acc, cur) => ({ ...acc, [cur]: input[cur] }), {})

I ended up just defining my own functions for what I needed. They're pretty gross since they use any, but they get the job done for me I think:

function fromEntries<T>(entries: [keyof T, T[keyof T]][]): T {
  return entries.reduce(
    (acc, [key, value]) => ({ ...acc, [key]: value }),
    <T>{}
  );
}

function toEntries<T>(obj: T): [keyof T, T[keyof T]][] {
  const entries: [string, any][] = Object.entries(obj);
  const entriesWithKeysLookedUp: [keyof T, T[keyof T]][] = entries.map(item => {
    const keyString = item[0];
    const value = item[1];
    return [<keyof T>keyString, value];
  });
  return entriesWithKeysLookedUp;
}

All I wanted to do was map an objects values. Ended up being way more annoying than it should have been. Does anyone know a better way? It's annoying that Object.entries returns [string, T] and not a keyof type since you know the first element in each of the array elements is a keyof something and not just a string.

@caseyhoward , Hi. I have a problem with the {} construct. TypeScript 3.8.3. http://www.typescriptlang.org/v2/en/play?useDefineForClassFields=true&allowUnreachableCode=true&allowUnusedLabels=true&downlevelIteration=true&target=99

Cannot use JSX unless the '--jsx' flag is provided.(17004)
'T' only refers to a type, but is being used as a value here.(2693)
JSX element 'T' has no corresponding closing tag.(17008)

Do you know the solution?

Go into tsconfig.json, and inside compilerOptions properties search for a property named lib. Add the string "esnext" in the array. It should look something like:
"lib": ["es2018", "dom", "esnext"]

Was this page helpful?
0 / 5 - 0 ratings

Related issues

blendsdk picture blendsdk  路  3Comments

DanielRosenwasser picture DanielRosenwasser  路  3Comments

dlaberge picture dlaberge  路  3Comments

Roam-Cooper picture Roam-Cooper  路  3Comments

Zlatkovsky picture Zlatkovsky  路  3Comments