79818596

Date: 2025-11-13 08:40:35
Score: 0.5
Natty:
Report link

Add a constraint for T. In TypeScript, you can add constraints that refer to each other for generic parameters.

const sumByField = <T extends {[P in K]: number}, K extends OnlyNumericKeys<T>>(data: T[], field: K) =>
    data.reduce(
        (acc, curr) => acc + curr[field],
        0
    );

https://tsplay.dev/wQa6Yw

Reasons:
  • Low length (0.5):
  • Has code block (-0.5):
  • Low reputation (0.5):
Posted by: Alexandroppolus