I am currently seeking a way to categorize a collection of partially applied functions where only the initial parameter(s) differ. In other words, this group consists of functions that can take any number of parameters of any type, but the first application must always result in a function of type (state: State) => Event[]
.
For instance, consider a set of functions like this:
const group = {
func1: () => (state: State) => Event[],
func2: (arg: string) => (state: State) => Event[],
func3: (arg: string, arg2: number) => (state: State) => Event[],
}
All these functions adhere to the following pattern (although not yet updated to accommodate multiple arguments):
export type Command = <T>(arg: T) => (state: State) => Partial<Event>[];
However, when attempting to define the group with typescript as shown below:
const command: Record<string, Command> = {
func1: () => (state: State) => [{}],
func2: (arg: string) => (state: State) => [{}],
func3: (arg: string, arg2: number) => (state: State) => [{}],
};
Typescript raises an error stating that type T is not assignable to type string.
Type '(arg: string) => (state: State) => {}[]' is not assignable to type 'Command'.
Types of parameters 'arg' and 'arg' are incompatible.
Type 'T' is not assignable to type 'string'
I understand the reason behind this assignment issue, but I am struggling to determine how I could properly type this grouping of partially applied functions. Essentially, my goal is to ensure that every function in this collection follows the Command type pattern, meaning it should be a partially applied function accepting parameters of any type that ultimately returns a function of type: (state: State) => Event[]
Is achieving such typing feasible? If so, what would be the correct approach?