FYI: A class implicitly exposes an interface that can be merged into - example:
type CompareFn = (lhs: number, rhs: number) => number; const sortWith: (a: readonly number[], f: CompareFn) => number[] = ( array, fn ) => array.slice().sort(fn); const ascending: CompareFn = (lhs, rhs) => lhs - rhs; const descending: CompareFn = (lhs, rhs) => rhs - lhs; class Container { readonly #values: readonly number[]; constructor(values: number[]) { this.#values = Object.freeze(values.slice()); } get values(): readonly number[] { return this.#values; } } interface Container { ascending(): number[]; descending(): number[]; } function asc(this: Container): number[] { return sortWith(this.values, ascending); } function desc(this: Container): number[] { return sortWith(this.values, descending); } Container.prototype.ascending = asc; Container.prototype.descending = desc; const container = new Container([10, 45, 15, 39, 21, 26]); console.log(container.values); console.log(container.ascending()); console.log(container.descending());
playground
Nice remark I learn something thank you bro !
Some comments have been hidden by the post's author - find out more
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
FYI: A class implicitly exposes an interface that can be merged into - example:
playground
Nice remark I learn something thank you bro !