Hello I am learning typescript and i follow this exercises: Link
I have problem understanding how should i proced with this example.
there is a code like this:
export function map(mapper, input) {
if (arguments.length === 0) {
return map;
}
if (arguments.length === 1) {
return function subFunction(subInput) {
if (arguments.length === 0) {
return subFunction;
}
return subInput.map(mapper);
};
}
return input.map(mapper);
}
and I am supposed to add types to this. I managed to create something like this:
export function map<T>(mapper: Function, input : T[]| any) : T[] | Function {
if (arguments.length === 0) {
return map;
}
if (arguments.length === 1) {
return function subFunction(subInput: T[]|any): T[] | Function {
if (arguments.length === 0) {
return subFunction;
}
return subInput.map(mapper);
};
}
return input.map(mapper);
}
typescript do not returns compilation errors now but i still fail the test. I do not understand what it is expected from me to make this work.
I can check suggested answer but whe i look at it this is dark magic for me.
I could check test.ts for what is expected but notation like const mapResult1 = map()(String)()([1, 2, 3]); is looking very strange for me.
Array.mapfunction receives a parameter and transforms it into another. So you need at least 2 generic parameters 1 is for arrays element type and 1 is for the output type.