I am trying to create a higher order function which can add some lines of code before and after the invocation and generate an extra value which needs to be shared with the wrapped function.
My code looks like the following right now.
interface Interface1 {
prop1: string;
}
const func1 = <P extends Interface1>(func2: (param: P) => any) => {
const str = 'something';
return (param: Omit<P, 'prop1'>) => { // Getting type error on this line
return func2({ ...param, prop1: str });
};
};
So in the above code I am receiving a type error
Argument of type 'Omit<P, "prop1"> & { prop1: string; }' is not assignable to parameter of type 'P'.
'Omit<P, "prop1"> & { prop1: string; }' is assignable to the constraint of type 'P', but 'P' could be instantiated with a different subtype of constraint 'Interface1'
I don't understand why can't Omit<P, "prop1"> & { prop1: string; } be equivalent of P here. Is this some sort of weird TypeScript issue, or is there something wrong that I am doing here. Of course I can just add as P to enforce the type, but I wanna understand why it's failing right now. Any idea?