Closed
Description
I have been looking at dependency injection and a few issues with refactoring in regards to _.pluck
. One of the primary things I run into is that I want these things to enforce the interface for refactoring.
A few examples
Interface used as a symbol for dependency injection
interface IExampleInterface {
interfaceProperty: string;
}
function angularInjection(...fields: string[]) {
return (target: Function) {
target.$inject = fields;
}
}
@angularInjection(nameof(IExampleInterface));
class Controller {
constructor(injectedExample: IExampleInterface) {
}
}
would compile to
function angularInjection() {
var fields = [];
for (var _i = 0; _i < arguments.length; _i++) {
fields[_i - 0] = arguments[_i];
}
return function (target) {
target.$inject = fields;
};
}
var Controller = (function () {
function Controller(injectedExample) {
}
Controller = __decorate([
angularInjection('IExampleInterface')
], Controller);
return Controller;
})();
In the case of an interface like pluck we could do something like this
interface IExampleInterface {
prop: string;
}
var items: IExampleInterface[] = [{prop: 'test1'},{prop: 'test1'}];
var props: string[] = _.pluck(items, nameof(IExampleInterface.prop));
would compile to
var items = [{prop: 'test1'}, {prop: 'test1'}];
var props = _.pluck(items, 'prop');
In this way we could enforce some level of type safety in common javascript reflection use cases.