Consider this function:
function invert(arr: string[]) {
let result = {};
arr.forEach((x, i) => {
result[x] = i;
})
return result; // insert type cast here
}
I am seeking a solution to have intellisense in VSCode indicate the available properties in the resulting object. For instance,
const example = invert(['foo', 'bar', 'baz'])
Desired auto-completion output: https://i.sstatic.net/JzNs5.png
My current approach involves casting result as {[K in T[number]]: K}
, where T
is an alias for typeof arr
. However, this method does not provide auto-completion.
How can I configure TypeScript to achieve this level of detail?
Note that the interface {[name]: number}
serves as a simplified example (due to NDA constraints), while the primary focus remains on mapping array elements to property names.