Refer to the snippet of code I have below:
// I'm a function that does not care what the type of the argument passed through to me is
const function1 = (parameter: any) => parameter;
// I'm a function that REQUIRES a number to function
const function2 = (parameter: number) => parameter + 5;
// I'm a function that REQUIRES a string to function
const function3 = (parameter: string) => parameter.split(':');
// I'm some combinatorial function that takes in a list of functions, and returns a "meta function"
function functionAggregator<A1, R1, R2>(f1: (a1: A1) => R1, f2: (a1: A1) => R2): (a1: A1) => [R1, R2] {
return (a1: A1) => [f1(a1), f2(a1)];
}
const validMetaFunction = functionAggregator(function1, function2);
// Valid because parameters of type number and any can overlap
const invalidMetaFunction = functionAggregator(function2, function3);
// Invalid because parameters of type number and string cannot overlap
const validFunctionResult = validMetaFunction(5);
// Valid result, because we passed in a number
const invalidFunctionResult = validMetaFunction('string');
// SHOULD be marked as an error, since type string cannot be passed into function2, which requires a type of number
On the very last line, I would expect validMetaFunction('string');
to be marked as invalid code, but because function1
has a parameter of type any
, the metaFunction's parameter type is widened to be of type any
. . .
Is there any way to solve this problem?