I want to find a way how to pass different objects to one same function and let the function process only inputs that are present in the given object. Specifically I am passing different Angular Components as this and am destructuring the object attributes.
export function filterVisualData({data, playersOn, typesOn, resultsOn, toBeRemoved, teamsOn, xGOn, shotsOn}) {
return data.filter(d => {
const result = d.success ? 'Successful' : 'Unsuccessful';
const players = playersOn.length === 0 || (playersOn.length > 0 && playersOn.includes(d.player_name));
const types = typesOn.length === 0 || (typesOn.length > 0 && typesOn.includes(capitalizeAllWords(d.type)));
const results = resultsOn.length === 0 || (resultsOn.length > 0 && resultsOn.includes(result));
const removed = !toBeRemoved.map(p => p.time).includes(d.time);
const shots = shotsOn.length === 0 || (shotsOn.length > 0 && shotsOn.includes(d.type));
const xG = xGOn.length === 0 || (xGOn.length > 0 && d.xG < Math.max(...xGOn));
const teams = teamsOn.length === 0 || (teamsOn.length > 0 && teamsOn.includes(d.team_name));
return players && types && results && removed && shots && xG && teams;
});
}
The problem is that some components are missing some attributes and I get this error
TS2345: Argument of type 'this' is not assignable to parameter of type '{ data: any; playersOn: any; typesOn: any; resultsOn: any; toBeRemoved: any; teamsOn: any; xGOn: any; shotsOn: any; }'. Type 'ShotChart' is not assignable to type '{ data: any; playersOn: any; typesOn: any; resultsOn: any; toBeRemoved: any; teamsOn: any; xGOn: any; shotsOn: any; }'. Property 'typesOn' is missing in type 'ShotChart'.
You can pass any object without Typescript complaining, by doing something like this :
So you get no error at compile time, but you will still get errors at runtime. If you don't pass
playersOn
, then you'll get aCannot read length of undefined
crash. You need to implement failsafes for each value.