Skip to content

Instantly share code, notes, and snippets.

@NachoToast
Created January 10, 2023 03:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NachoToast/01145384c9746db589c2fefcf9b5b574 to your computer and use it in GitHub Desktop.
Save NachoToast/01145384c9746db589c2fefcf9b5b574 to your computer and use it in GitHub Desktop.
Existing Class Extending in Typescript
// typing of implementation is not checked by the compiler, only calls to it
declare global {
interface Number {
between: (min: number, max: number) => boolean;
}
}
Object.defineProperty(Number.prototype, 'between', {
value(min: number, max: number): boolean {
return this >= min && this <= max;
},
});
const p = 5;
console.log(p.between(5, 10)); // true
console.log(p.between(0, 4)); // false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment