Skip to content

Instantly share code, notes, and snippets.

Created December 20, 2019 11:14
What would you like to do?
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>[]> =
[K in keyof T]: T[K] extends KeyValue<KEY, Val>
? Record<T[K][KEY], T[K]>
: never
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