Skip to content

Commit e0662d6

Browse files
committed
Add stronger typing for Object.fromEntries
1 parent fbe70d9 commit e0662d6

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

typings/object.d.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,24 @@ type ObjectKeys<T> = T extends object
99

1010
interface ObjectConstructor {
1111
keys<T>(o: T): ObjectKeys<T>;
12+
13+
fromEntries<
14+
V extends PropertyKey,
15+
T extends [readonly [V, any]] | Array<readonly [V, any]>,
16+
>(
17+
entries: T,
18+
): Flatten<UnionToIntersection<FromEntries<T[number]>>>;
1219
}
20+
21+
// From https://github.com/microsoft/TypeScript/issues/35745#issuecomment-566932289
22+
type UnionToIntersection<T> = (T extends T ? (p: T) => void : never) extends (
23+
p: infer U,
24+
) => void
25+
? U
26+
: never;
27+
type FromEntries<T extends readonly [PropertyKey, any]> = T extends T
28+
? Record<T[0], T[1]>
29+
: never;
30+
type Flatten<T> = object & {
31+
[P in keyof T]: T[P];
32+
};

0 commit comments

Comments
 (0)