This snippet of code is expanding the definition of the global Array
:
// arrayExtend.ts
export {}
declare global {
interface Array<T> {
customIndexOf(e: T, desc: boolean): number
addToSortedArray(e: T, desc: boolean): T[]
}
}
if (!Array.prototype.customIndexOf) {
Object.defineProperty(Array.prototype, 'customIndexOf', {
enumerable: false,
writable: false,
configurable: false,
value: function customIndexOf<T>(this: T[], e: T, desc: boolean) {
...
No errors are detected by the compiler regarding missing functions, however, an error is encountered during execution:
TypeError: Cannot redefine property: customIndexOf
at src/arrayExtend.ts:36
34 |
35 | if (!Array.prototype.addToSortedArray) {
> 36 | Object.defineProperty(Array.prototype, 'customIndexOf', {
| ^
37 | enumerable: false,
38 | writable: false,
39 | configurable: false,
Changing the configurable
flag to true
results in another error when attempting to use addToSortedArray
:
TypeError: testData.addToSortedArray is not a function