I am working on a function that takes an array of strings and generates an object where the strings are used as keys with a value of true assigned to each.
Here is the code snippet for that:
return keys.reduce((result, key) => {
result[key] = true;
return result;
}, {});
I want to implement this function using TypeScript so that, when calling it, it will automatically detect that the returned object will have keys corresponding to the input array of strings.
Is there a way to accomplish this? I wish for the following code to be operational:
const result = myFunction(['key1', 'key2'])
When this is done, I hope TypeScript can identify result as an object with keys 'key1' and 'key2'.
Thank you to anyone who can offer assistance!