Types or no types

Here are two versions of some example code I've recently been working on.

One has types and uses TypeScript, the other is JavaScript and has no types.

Which do you prefer and why?

TypeScript (with types)

add(...numbers: number[]): number {
  return numbers.reduce((a: number, b: number) => a + b, 0);
}

subtract(...numbers: number[]): number {
  let total = numbers[0];

  for (var i = 1, length = numbers.length; i < length; i++) {
    total -= numbers[i];
  }
  return total;
}

JavaScript (no types)

add(...numbers){
  return numbers.reduce((a, b) => a + b, 0);
}

subtract(...numbers) {
  let total = numbers[0];

  for (var i = 1, length = numbers.length; i < length; i++) {
    total -= numbers[i];
  }

  return total;
}

- Oliver

Was this interesting?

Sign up here and get more like this delivered straight to your inbox every day.

About me

Picture of Oliver

I'm an Acquia-certified Drupal Triple Expert with 17 years of experience, an open-source software maintainer and Drupal core contributor, public speaker, live streamer, and host of the Beyond Blocks podcast.