apollographql / apollo-tooling

✏️ Apollo CLI for client tooling (Mostly replaced by Rover)
https://apollographql.com
MIT License
3.04k stars 468 forks source link

apollo-env polyfill fromentries does not match Typescript ES2019 #2041

Open Sytten opened 4 years ago

Sytten commented 4 years ago

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!

Dremora commented 2 years ago

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.