Lately, I've been delving into Typescript Decorators to address a specific issue in my application. Using a JS bridge to communicate TS code between Android and iOS, we currently define functions as follows:
index.js
import foo from './bar'
global.App = function() {
this.foo = foo;
};
The above snippet enables the foo
function to be accessed on the native side of the bridge.
I attempted to create a decorator for the foo
method that would automatically "register" itself within global.App
, but unfortunately, I didn't succeed in achieving this goal.
Below is the working decorator:
export const Task = (target, propertyKey, descriptor) => {
let originalMethod = descriptor.value;
descriptor.value = (...args) => originalMethod.apply(target, args);
if (!global.App) {
global.App = function () {
this.foo = descriptor.value;
};
}
return descriptor;
}
Is there a way to add another method to the global.App
function?