does this meet your expectations:
const ob = arrayToObject(['a', 'b', 'c']);
type Convert<T extends ReadonlyArray<string>> = {
[P in T[number]]: string
}
type Result = Convert<['foo', 'bar']> // {foo: string, bar: string)
function arrayToObject<T extends ReadonlyArray<string>>(args: readonly string[]): Convert<T> {
return args.reduce((acc, elem) => ({...acc, [elem]: 'hello' }), {} as Convert<T>)
}
UPDATE
// Please keep in mind, array is index based data structure. Index has `number` type
type Arr = readonly [1,2,3,4,5]
// So, You can try to get value by index
type Value1 = Arr[0] // 1
type Value2 = Arr[1] // 2 ... etc
type Value3 = Arr[0|1] // 1|2
// This is how distributive types work
type Value4 = Arr[number] // 5|1|2|3|4
// TS know that array has numbers as indexes, so he replace `number` with allowed // indexes. I'd willing to bet that TS compiler works much more complicated, but // this is how I understand it
arr = ["a", "b", "c"] as const. However, if you don't have the contents at compile time (e.g.,["a", "b", prompt("enter a letter")]) then I don't think TS has any way of expressing an object which has keys based on the contents of the array.as constassertion. I'll write up an answer for this.