2

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;
}
2
  • Could you try var translateFilter:any Commented Jul 21, 2015 at 13:57
  • @vmaksym no ****ing way. TypeScript's aim is not to avoid type checking by using any. Commented Jul 21, 2015 at 13:59

1 Answer 1

6

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.