I am looking for a way to not provide an empty array for a generic function Parameter<F>-typed parameter when F does not receive parameters.
The following working example shows the current state
type A<F extends (...args: any[]) => any> = {
shouldPrintHello: boolean;
params: Parameters<F>;
};
const wrappingFunction = <F extends (...args: any[]) => any>(sentFunction: F, defaultParams: A<F>) => {
const innterFunction = (...args: Parameters<F>) => {
if (defaultParams.shouldPrintHello) console.log("hello");
sentFunction(args);
return;
};
const defaultValue = sentFunction(defaultParams);
return innterFunction;
};
const f1 = wrappingFunction(
(arg0: string) => {
return;
},
{ shouldPrintHello: true, params: ["defaultString"] }
);
const f2 = wrappingFunction(
() => {
return;
},
{ shouldPrintHello: true, params: [] }
);
f1("a string");
f2();
Desired (pseudo) code changes:
type A<F extends (...args: any[]) => any> = {
shouldPrintHello: boolean;
params: Parameters<F> === [] ? undefined : Parameters<F>;
};
const f2 = wrappingFunction(
() => {
return;
},
{ shouldPrintHello: true }
);
f2();
extendskeyword?