Created
December 20, 2019 11:14
-
-
Save RubaXa/be9ca8165512d28ff2bc5170392018d4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Key = string; | |
type Val = string | number; | |
type KeyValue<K extends Key, V extends Val> = { | |
[X in K]: V; | |
} | |
type CompostedObject<KEY extends Key, T extends KeyValue<KEY, Val>[]> = | |
FlattenObject< | |
ToIntersect< | |
{ | |
[K in keyof T]: T[K] extends KeyValue<KEY, Val> | |
? Record<T[K][KEY], T[K]> | |
: never | |
}[number] | |
> | |
> | |
; | |
function composeObjects< | |
K extends Key, | |
T extends KeyValue<K, string>[], | |
>(key: K, ...objects: T): CompostedObject<K, T> { | |
return objects.reduce((result, obj) => { | |
result[obj[key] as any] = obj; // Да, тут мне ts сказал «давай, досвидания» | |
// и я написал `any`, но это временно!1 | |
return result; | |
}, {} as CompostedObject<K, T>) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment