Search code examples
typescriptwebstormdurandal

How to avoid "unresolved function" error with TypeScript mixins?


WebStorm is reporting an unresolved function when I use Durandal's Events.includeIn() mixin method on a TypeScript class:

class Foo {
  bar() {
    ...
    this.trigger('myEvent', payload);  // trigger is unresolved function
    ...
  }
}

Events.includeIn(Foo);

Is there a way to resolve this without using WebStorm to suppress each unresolved call?


Solution

  • In order to call the trigger method on the Foo class, you will need to inform the TypeScript compiler about the trigger method. You can do this by declaring the methods that will exist after the includeIn call in the class definition:

    class Foo {
    
      // I'm not sure of the exact parameter/return types
      trigger: (eventName: string, payload: any) => void;
    
      bar() {
        ...
        this.trigger('myEvent', payload);  // now this function exists
        ...
      }
    }
    
    Events.includeIn(Foo);