Flow: Static methods on an interface

Created on 7 Jul 2016  路  4Comments  路  Source: facebook/flow

Say I have an interface I want a class to define, and one of the methods/properties is static. What's the right way to define that?

I tried this:

export type PlanAndTargetT = {
  plan:PlanT,
  target:mixed
};

export interface PerformerI {
  static canHandle(planAndTarget:PlanAndTargetT):boolean;
  someProperty:string;
}

and Flow didn't know how to parse it. This appears to work, but it seems hacky-at-best:

interface PerformerInstancesI {
  someProperty:string;
}

type PerformerStaticsT = {
  canHandle(planAndTarget:PlanAndTargetT):boolean;
}

export type PerformerI = PerformerInstancesI&PerformerStaticsT;
interfaces

Most helpful comment

Static methods in ES6 are perfectly analogous to ordinary methods, unlike in Java or C++. Further, they're accessible within ordinary methods through the constructor property. It therefore makes perfect sense to support them on interfaces. Submitting pull request.

All 4 comments

ProTip: use '''typescript (I mean backticks) at the starting of your code block to get syntax highlighting.

See also #803 and #1704.

Static methods on interface doesn't make much sense. If you want your class to have some static methods, then you need to create a separate type containing those methods.

Static methods in ES6 are perfectly analogous to ordinary methods, unlike in Java or C++. Further, they're accessible within ordinary methods through the constructor property. It therefore makes perfect sense to support them on interfaces. Submitting pull request.

Was this page helpful?
0 / 5 - 0 ratings