In the development of my middleware engine, I have incorporated various generic arguments that are specific to the particular implementation in use.
export type Middleware<
Store = never,
Args = unknown,
Response = unknown
> = (
context: {
endpoint: string
args: Args
locals: {
store?: Store,
[key: string]: unknown
}
}
) => Promise<Response|void>|Response|void
The range of generic arguments is continuously expanding as I enhance the Middleware
with more features. At times, a store
may not be present while other arguments are required. While defaults and parameter order can be relied upon, it becomes cumbersome for developers when listing arguments in the correct sequence and specifying unknown
or never
for those preceding others that need definition.
I am exploring alternative methods of supplying arguments to generics without depending on a fixed order, allowing for any number of relevant generic arguments while defaulting others to a predefined value in the most general scenario. Could a mapping approach potentially offer a solution?