-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreadonlyArrays.ts
More file actions
23 lines (17 loc) · 849 Bytes
/
readonlyArrays.ts
File metadata and controls
23 lines (17 loc) · 849 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
function mutatedSortAndReverse(input: number[]): number[] {
return input.sort().reverse();
}
const beforeMutatingFunction = [1, 2, 3, 4, 5];
const afterMutatingFunction = mutatedSortAndReverse(beforeMutatingFunction);
console.log(beforeMutatingFunction); // [5,4,3,2,1];
console.log(afterMutatingFunction); // [5,4,3,2,1];
function immutatedSortAndReverse(input: readonly number[]): number[] {
// return input.sort().reverse(); // Compile Error
return input.slice().sort().reverse();
}
const beforeImmutatingFunction = [1, 2, 3, 4, 5];
const afterImmutatingFunction = immutatedSortAndReverse(beforeImmutatingFunction);
console.log(beforeImmutatingFunction); // [1,2,3,4,5]
console.log(afterImmutatingFunction); // [5,4,3,2,1]
type ReadonlyNumberArray = readonly number[];
type ReadonlyNumberArrayAlternate = ReadonlyArray<number>;