Skip to content

Instantly share code, notes, and snippets.

@chpio
Created May 28, 2021 10:41
Show Gist options
  • Save chpio/1f2ee880917322dc7e0769688338c03c to your computer and use it in GitHub Desktop.
Save chpio/1f2ee880917322dc7e0769688338c03c to your computer and use it in GitHub Desktop.
Object.fromEntries typescript
interface ObjectConstructor {
fromEntries<T extends readonly [PropertyKey, unknown]>(
entries: Iterable<T>,
): { [K in T[0]]?: Extract<T, readonly [K, unknown]>[1] };
}
@chpio
Copy link
Author

chpio commented May 28, 2021

Seams to not work properly if there are multiple keys having the same content type, eg:

    const test = Object.fromEntries(
        ([0, 1, 2]).map((i) => {
            return <const>[
                i % 2 === 0 ? "foo" : "bar",
                i,
            ];
        })
    );

results in:

const test: {
    foo?: undefined;
    bar?: undefined;
}

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