daily-email: add 2023-09-15
Types or no types
This commit is contained in:
parent
2543521576
commit
cdf3223a75
53
src/content/daily-email/2023-09-15.md
Normal file
53
src/content/daily-email/2023-09-15.md
Normal file
|
@ -0,0 +1,53 @@
|
|||
---
|
||||
title: >
|
||||
Types or no types
|
||||
pubDate: 2023-09-15
|
||||
permalink: >
|
||||
archive/2023/09/15/types-or-no-types
|
||||
tags:
|
||||
- software-development
|
||||
- types
|
||||
- JavaScript
|
||||
- TypeScript
|
||||
---
|
||||
|
||||
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;
|
||||
}
|
||||
```
|
Loading…
Reference in a new issue