I want to make an array, which will specify, which strings will index properties inside object and that object properties only contain functions.
What I do understand, is that I can create union type from just array:
My working case is a reducer action (instruction) in React reducer (but it can be used elsewhere):
const InstructionsArray = [
'HANDLE_BUTTON_CONVERT_INPUT',
'HANDLE_CHECKBOX_CONVERT_ON_INPUT',
'HANDLE_CHECKBOX_DISPLAY_TAGS',
'HANDLE_TEXTAREA_INPUT',
'HANDLE_RUNTIME'
] as const;
Then I have type object with property:
instruction: typeof InstructionsArray[number]
Of course, normal array would allow us to use any string as indexes, but since we casted it as const, it now only allows for the strings, we specified in array.
What I actually want, is to have an object, which will have some property names as string and all properties will have value of function. I would like to specify only those property names (string), but as every value will be a function, I would not like to specify it more than once, so it can be taken as granted.
Current code looks like that:
const changersLabels = [
'inventory',
'quest'
] as const;
But the 'same' way does not work on object like that:
keyboardRouter(changers: {[key:typeof changersLabels[number]:Function]}) {
//some stuff
}
What am I doing wrong and is that even possible?