1

I'm having problems with the piece of code below:

CoreModule.filter('translateEnum', ['$parse', '$translate', function(
  $parse: angular.IParseService,
  $translate: angular.translate.ITranslateService
) {
  var translateFilter = function (translationId, namespace, interpolateParams, interpolation): string {
    if (!translationId) {
      return '';
    } else {
      if (!angular.isObject(interpolateParams)) {
        interpolateParams = $parse(interpolateParams)(this)
      }
      return $translate.instant( namespace + '.' + translationId, interpolateParams, interpolation)
    }
  }
  translateFilter.$stateful = true;
  return translateFilter;
}]);

I'm getting compilation error constantly:

>> app/modules/core/filters/filters.ts(47,19): error TS2339: Property '$stateful
' does not exist on type '(translationId: any, namespace: any, interpolateParams
: any, interpolation: any) => string'.

The problem is that I need to assing $stateful: boolean property to a function object and I've got no idea how to do this in typescript.


Here are some tries I've done, but all of them failed:

interface StatefulFunction {
  (translationId, namespace, interpolateParams, interpolation): string;
  $stateful: boolean;
}

or

interface StatefulFunction {
  (...args: any[]): any;
  $stateful: boolean;
}

or even

interface StatefulFunction extends Function {
  $stateful: boolean;
}
ducin
  • 25,621
  • 41
  • 157
  • 256

1 Answers1

4

You can do something like this:

interface StatefulFunction {
    (): string;
    $stateful: boolean;
}

var translateFilter = <StatefulFunction>function (/*...*/) {
    /*...*/
};

Adapted from this answer: https://stackoverflow.com/a/18640025/373655

Community
  • 1
  • 1
rob
  • 17,995
  • 12
  • 69
  • 94