I have a function that accept string or string array and return an object with keys of those string.
Here is the dummy function:
type myType<T extends string> = { [K in T]: string }
const myFunc = <T extends string>(param: T | T[]): myType<T> => {
let result = <myType<T>>{}
// some codes here...
return result;
}
With the above code, I already achieved this:
let val = myFunc(['foo', 'bar']);
val.foo // valid
val.other // invalid
But if I pass a variable to the function, all string keys are valid:
let variable = ['foo', 'bar'];
let val = myFunc(variable);
val.foo // valid
val.other // valid
Is there a workaround so I could pass a variable but still working as what I expected?
string extends T ? never :but I don't know what the syntax is for that