In my Angular 8.2/Typescript 3.4.3 app, I need to generate an array.map callback dynamically, but the TS linter is barking about that function. Here is an example snippet I contrived to illustrate:
export type Role = [string, number];
export type BaseRole = [string, number[]];
const roles: BaseRole[] = [
[ 'hero', [100, 200, 300]],
[ 'villain', [300, 600, 900]],
[ 'minion', [20, 40, 60]]
];
function roleMapper(i: number): Function {
return ([role, strengths]) => [role, strengths[i]];
}
function getRolesAtLevel (level): Role[] {
return roles.map(roleMapper(level)); // <-- Linter warning occurs here
}
let myRoles = getRolesAtLevel(1);
Above I want to map an array of "BaseRoles" down to "Roles" based on user input. The linter complains about return roles.map(roleMapper(level)); (line 16) with the follow message:
Argument of type 'Function' is not assignable to parameter of type '(value: [string, number[]], index: number, array: [string, number[]][]) => [string, number]'.
I observe that the type '(value ... index ... array)' would be that of a map callback. The function I've provided is typed Function and supports that interface, so my questions are:
- Must I be explicit about the interface of the function when using the Function type?
- Is there another notation that I should have used to designate a "map" callback?
Thanks..