Problem Statement
Build an array utility that accepts only homogeneous arrays; explain failure cases.
Explanation
Constrain the element type with a single generic so all items share one type. If a caller mixes types (e.g., `number | string`), the element type widens and the utility can refuse it via constraints.
Code Solution
SolutionRead Only
function average<T extends number>(items: readonly T[]): number {
return items.reduce((a, b) => a + b, 0) / items.length;
}
// average([1,2,3]); // ✅
// average([1,'2',3]); // ❌ string not assignable to number