Hey there! I've been diving into learning typescript and have been working through some exercises. If you're curious, you can check out the exercise here.
I'm a bit stuck on grasping how to approach this particular example.
Here's the code snippet that's giving me trouble:
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);
}
I've tried adding types to it like so:
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);
}
Although TypeScript isn't throwing any compilation errors, I'm still unable to pass the test cases. I'm feeling a bit lost on what exactly is expected for this to work.
I could peek at the provided solution, but honestly, it feels like magic to me at this point.
Looking into the test.ts
file for clues seems daunting with expressions like
const mapResult1 = map()(String)()([1, 2, 3]);
. It's all a bit overwhelming!