简体   繁体   中英

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?

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM