Suppose I want to execute the generateFunction() method which will yield the following function:
// The returned function
const suppliedFunction = <T>(args: T) => {
return true;
}; // The returned function
// This is how it can be used
suppliedFunction({ prop: "value" });
I have chosen to construct the generatorFunction like this:
// Function generator, returning the supplied function from the parameter
// The supplied function should return a boolean
function generateFunction(fn: <T>(args: T) => boolean) {
return fn;
}
// However, I encountered an error with this code
// Property 'prop' does not exist on type 'unknown'
const resultFunction = generateFunction(({ prop }) => {
return true;
});
My intention is to utilize resultFunction with type inference derived from the generated function (instead of explicitly specifying the type)
// This should result in an error
// Argument of type 'string' is not assignable to parameter of type '{ prop: any; }'
resultFunction('text');
// This should work fine
resultFunction({ prop: "winter" });
How can we correctly implement these generics for my scenario? Thank you in advance.