Created
February 27, 2021 22:08
-
-
Save FFKL/441cd29b46215d0e2903bfc7bb917c68 to your computer and use it in GitHub Desktop.
Implementation of SortedArray wrapper. Thanks to @JSMonk
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SortedArray<T> { | |
static of<T>(array: Array<T>): SortedArray<T> { | |
return new SortedArray(array); | |
} | |
private readonly array: Array<T>; | |
private constructor(array: Array<T>, sortingWith?: (a: T, b: T) => number) { | |
this.array = Array.from(array).sort(sortingWith); | |
} | |
} | |
function expectSortedArray(arr: SortedArray<number>) { | |
// do something | |
} | |
expectSortedArray([1, 2]); // Error! | |
expectSortedArray(SortedArray.of([1, 2])); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment