Skip to content

Commit f8afdd7

Browse files
pokeyfidgetingbits
authored andcommitted
Add stronger typing for Object.fromEntries (cursorless-dev#1944)
## Checklist - [ ] I have added [tests](https://www.cursorless.org/docs/contributing/test-case-recorder/) - [ ] I have updated the [docs](https://github.com/cursorless-dev/cursorless/tree/main/docs) and [cheatsheet](https://github.com/cursorless-dev/cursorless/tree/main/cursorless-talon/src/cheatsheet) - [ ] I have not broken the cheatsheet
1 parent 818a2d7 commit f8afdd7

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)