In search of a universal type to implement in my actions. Actions can vary from simple functions to functions that return another function, demonstrated below:
() => void
() => (input: I) => void
An Action type with a conditional generic Input has been crafted, as follows:
type ConditionalInput<I = null> = I extends null
? () => void
: () => (input: I) => void;
This type operates smoothly without an Input being passed:
const action1: ConditionalInput = () => { } // Perfect;
Moreover, when a singular Input is provided:
const action2: ConditionalInput<string> = () => str => { } // Excellent;
However, if a union of Inputs is used,
const action3: ConditionalInput<string | number> =
() => strOrNum => { } // The input becomes any!!;
A live demonstration with the code can be accessed via this link: TS Playground
If conditionals are removed, the union functions seamlessly:
type NonConditionalInput<I> = () => (input: I) => void;
const action4: NonConditionalInput<string | number> =
() => strOrNum => { } // No issues;