I created a function that can accept either a string or an array of strings, and it returns an object with keys based on those strings.
Below is an example of the function:
type MyType<T extends string> = { [K in T]: string }
const myFunction = <T extends string>(param: T | T[]): MyType<T> => {
let result = <MyType<T>>{}
// some code here...
return result;
}
Using the above code snippet, I have successfully achieved this:
let values = myFunction(['apple', 'orange']);
values.apple // valid
values.other // invalid
However, when I pass a variable to the function, all string keys become valid:
let fruitArray = ['apple', 'orange'];
let values = myFunction(fruitArray);
values.apple // valid
values.other // valid
Is there a way to address this issue so that I can use a variable while still maintaining the expected behavior?