oliverdavies.uk/source/_daily_emails/2023-09-15.md

54 lines
940 B
Markdown
Raw Normal View History

2024-01-03 20:00:00 +00:00
---
title: >
2024-09-08 22:09:54 +00:00
Types or no types
2024-01-03 20:00:00 +00:00
pubDate: 2023-09-15
permalink: >-
daily/2023/09/15/types-or-no-types
2024-01-03 20:00:00 +00:00
tags:
2024-09-08 22:09:54 +00:00
- software-development
- types
- JavaScript
- TypeScript
2024-01-03 20:00:00 +00:00
---
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)
```js
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)
```js
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;
}
```