Skip to content

Instantly share code, notes, and snippets.

@RubaXa
Created December 20, 2019 11:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RubaXa/be9ca8165512d28ff2bc5170392018d4 to your computer and use it in GitHub Desktop.
Save RubaXa/be9ca8165512d28ff2bc5170392018d4 to your computer and use it in GitHub Desktop.
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