Skip to content

Instantly share code, notes, and snippets.

@FFKL
Created February 27, 2021 22:08
Show Gist options
  • Save FFKL/441cd29b46215d0e2903bfc7bb917c68 to your computer and use it in GitHub Desktop.
Save FFKL/441cd29b46215d0e2903bfc7bb917c68 to your computer and use it in GitHub Desktop.
Implementation of SortedArray wrapper. Thanks to @JSMonk
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