Skip to content

Suggestion: Interfaces as string literals #2902

Closed
@park9140

Description

@park9140

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already createdSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions