No need to know React, just some background: in my use case, I want to amplify user specified React.ComponentClass with my custom Higher Order Component. In order to do so, I want user to also send me list of names of the particular props my Higher Order Component will inject. That would be done like this:
function listToComponentProps<TComponentProps, TKey = keyof TComponentProps>(props: Array<TKey>) {
// Logic here, not important for the example
}
interface TestProps {a: number, b: Function, c: () => number}
listToComponentProps<TestProps>(['a', 'b', 'c'])
The keyof keyword handles the constraint for me.
Example inputs for listToComponentProps<TestProps> would be
- valid:
['b'],['b', 'c'],['c'] - invalid
['a'],['a', 'b'],['a', 'b', 'c'](a is number, not a function)['d'],['d', 'c'](d is not part of the interfaceTestProps
The problem is, I want to restrict the props parameter not only to be key of TComponentProps, but also such key that the corresponding type in TComponentProps is Function (so that 'a' would be an invalid option detected by typescript compiler). How can one achieve such task?