Created
January 10, 2023 03:50
-
-
Save NachoToast/01145384c9746db589c2fefcf9b5b574 to your computer and use it in GitHub Desktop.
Existing Class Extending in Typescript
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
// 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