You could start by making T really generic:
function toMap<T extends { type: string }>(...args: T[]): { [type: string]: T } {
return args.reduce((res, t) => ({
...res,
[t.type]: t
}), {});
}
To be then able to really narrow down the types, you have to type generic types for variable arguments, e.g. toMap<A>(arg1: A), toMap<A, B>(arg1: A, arg2: B).
There are two downsides though:
1) You have to create these overloads for any number of arguments,
however that is common in Typescript (see Object.assign declaration).
2) Typescript types { type: "test" } as { type: string } by default (which is wanted in 99% of the cases), however therefore we can't infer the keys type to "test" directly. To solve this, we have to typecast the string literal to a narrowed down string type { type: "test" as "test" }.
// generic overload for one argument
function toMap<A>(arg: A): { [K1 in O<A>]: A };
// generic overload for two arguments:
function toMap<A, B>(arg: A, arg2: B): { [K in O<A>]: A } & { [K in O<B>]: B };
// generic overload for three arguments:
function toMap<A, B, C>(arg: A, arg2: B, arg3: C): { [K in O<A>]: A } & { [K in O<B>]: B } & { [K in O<C>]: C };
// ... repeat for more arguments
// implementation for all kind of args
function toMap<T extends { type: string }>(...args: T[]): { [type: string]: T } {
return args.reduce((res, t) => ({
...res,
[t.type]: t
}), {});
}
// Infers the type of "type", which has to be a string, from a given object
type O<V> = V extends { type: infer K } ? K extends string ? K : never : never;
// Narrow down a.type to be "test" instead of string
const a = { type: "test" as "test" }
const b = { type: "test2" as "test2", v: 1 };
const test = toMap(a);
const test2 = toMap(a, b);
console.log(
test2.test2.v, // works!
test2.whatever, // doesnt!
test2.test2.k // doesnt!
);
Try it!