mvp-di
Version:
A simple DI package inspired by the MVP pattern. It can inject the presentation layer in React.js/React Native applications.
19 lines (17 loc) • 601 B
text/typescript
export function bind<T extends Function>(target: Object, propertyKey: string, descriptor: TypedPropertyDescriptor<any>): TypedPropertyDescriptor<any> {
if (!descriptor || (typeof descriptor.value !== 'function')) {
throw new TypeError(`Only methods can be decorated with @bind. <${propertyKey}> is not a method!`);
}
return {
configurable: true,
get(this: T): T {
const bound: T = descriptor.value!.bind(this);
Object.defineProperty(this, propertyKey, {
value: bound,
configurable: true,
writable: true
});
return bound;
}
};
}