apollo-tooling icon indicating copy to clipboard operation
apollo-tooling copied to clipboard

apollo-env polyfill fromentries does not match Typescript ES2019

Open Sytten opened this issue 5 years ago • 1 comments

Hi!

Currently, Object.fromentries in the apollo-env polyfill is declared as:

fromEntries<K extends string, V>(map: [K, V][]): Record<K, V>;

While the Typescript official method is:

fromEntries<T = any>(entries: Iterable<readonly [PropertyKey, T]>): { [k: string]: T };

Since apollo-env is declared globally, it will overwrite the official method. And the official typing is nicer to work with. Would it be possible to align the typing with the official one and/or remove it since it is now part of >ES2019.

Thanks!

Sytten avatar Jul 16 '20 15:07 Sytten

It's also incorrect.

See the following code:

type Key = 'foo' | 'bar';
const keys: Key[] = ['foo'];

const b = Object.fromEntries<Key, number>(
  keys.map((key) => {
    return [key, 1];
  }),
);

console.log(b.bar);

The type of b.bar is number but it evaluates to undefined.

The same code behaves as expected with official typing.

Both examples use noUncheckedIndexedAccess compiler flag.

Dremora avatar Jun 17 '22 12:06 Dremora