Typescript and Array.reduce()

Ah, funnily enough I had tried a generic type guard to just check if all elements are of the same type… but this wouldn’t solve the issue:

function addUp(input: Summable) {
  const [head, ...tail] = input
  
  if (tail.some(el => typeof el !== typeof head)) {
    throw new TypeError()
  }

  return input.reduce((acc, val) => acc + val)
}

… maybe to abstract or something. oO

Yes that was the idea… FWIW I also found a neat suggestion for a generic XOR type, but the issue remained the same for typed arrays. So yeah, I concur it might be a bug.

Not sure, but probably related to the same bug?

1 Like