Skip to content

Commit

Permalink
Add stronger typing for Object.fromEntries (cursorless-dev#1944)
Browse files Browse the repository at this point in the history
  • Loading branch information
pokey authored and fidgetingbits committed Nov 3, 2023
1 parent 818a2d7 commit f8afdd7
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions typings/object.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,24 @@ type ObjectKeys<T> = T extends object

interface ObjectConstructor {
keys<T>(o: T): ObjectKeys<T>;

fromEntries<
V extends PropertyKey,
T extends [readonly [V, any]] | Array<readonly [V, any]>,
>(
entries: T,
): Flatten<UnionToIntersection<FromEntries<T[number]>>>;
}

// From https://github.com/microsoft/TypeScript/issues/35745#issuecomment-566932289
type UnionToIntersection<T> = (T extends T ? (p: T) => void : never) extends (
p: infer U,
) => void
? U
: never;
type FromEntries<T extends readonly [PropertyKey, any]> = T extends T
? Record<T[0], T[1]>
: never;
type Flatten<T> = object & {
[P in keyof T]: T[P];
};

0 comments on commit f8afdd7

Please sign in to comment.