From 4e65c84c768da1eabeca552df54ea1ed78812f8e Mon Sep 17 00:00:00 2001 From: Sam Wilkins Date: Tue, 15 Oct 2019 18:35:56 -0400 Subject: fixed fatal mobx extension-related error, still can use extensions --- src/extensions/ArrayExtensions.ts | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/extensions/ArrayExtensions.ts b/src/extensions/ArrayExtensions.ts index 422a10dbc..8e125766d 100644 --- a/src/extensions/ArrayExtensions.ts +++ b/src/extensions/ArrayExtensions.ts @@ -1,13 +1,37 @@ -function Assign() { +export default class ArrayExtension { + private readonly property: string; + private readonly body: (this: Array) => any; + + constructor(property: string, body: (this: Array) => any) { + this.property = property; + this.body = body; + } + + assign() { + Object.defineProperty(Array.prototype, this.property, { + value: this.body, + enumerable: false + }); + } - Array.prototype.lastElement = function () { +} + +/** + * IMPORTANT: Any extension you add here *must* have a corresponding type definition + * in the Array interface in ./General/ExtensionsTypings.ts. Otherwise, + * Typescript will not recognize your new function. + */ +const extensions = [ + new ArrayExtension("lastElement", function () { if (!this.length) { return undefined; } - const last: T = this[this.length - 1]; - return last; - }; + return this[this.length - 1]; + }) +]; +function Assign() { + extensions.forEach(extension => extension.assign()); } export { Assign }; \ No newline at end of file -- cgit v1.2.3-70-g09d2