Issue
I got Eslint warning in following TypeScript code:
"Missing return type on function.eslint(@typescript-eslint/explicit-function-return-type)" The warning refers the lambda in map function.
private headerSelectionChanged(event): void {
const { checked } = event.syntheticEvent.target;
const newGridData = this.props.gridData.map(item => Object.assign(item, { selected: checked }));
this.updateGridData(newGridData);
}
What is wrong? How can I fix it?
Solution
The warning shows because your mapping function does not specify a return type. You have to specify the return type like this:
const newGridData = this.props.gridData.map((item): any => Object.assign(item, { selected: checked }));
Keep in mind that Object.assign
modifies the target instead of copying it. If you do not want this.props.gridData
to be modified, you should swap item
and { selected: checked }
const newGridData = this.props.gridData.map((item): any => Object.assign({ selected: checked }, item));
or use the newer object spread operator, if available:
const newGridData = this.props.gridData.map((item): any => ({ ...item, selected: checked }));
Answered By - corschdi
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.