如何为具有字段的函数声明Flow类型?

Ric*_*ast 11 javascript flowtype flow-typed

我正在尝试编写一个Javascript项目,其中包含严格的流式输入.我也有依赖big-integer.flow-typed遗憾的是,没有预设的流量注释,Google没有提供有关该主题的任何有用信息.

像许多JavaScript包一样,big-integer导出一个通常被称为的函数bigInt.这可以直接调用,就像这样:bigInt(13),bigInt("134e134")等等,这就造成它是大的整数(我决定叫"下课"叫"的BigInteger"基于文档此功能的返回值的类型的对象- - 但我不认为内部实际上使用类,因为我相信包在ES6之前出来了).

这适用于函数的输出,我可以将方法附加到该类,我们都很好.但是,bigInt它本身有一些方法,例如bigInt.lcm(123, 234).我该如何记录这个?

declare module "big-integer-types" {
  declare class BigInteger {
    add(addend: BigIntInput): BigInteger;
    minus(subtractand: BigIntInput): BigInteger;
    /* snip */
  }
  declare type BigIntInput = number | string | BigInteger;
  declare type BigIntFn = (void | number | string | BigInteger) => BigInteger;
}

declare module "big-integer" {
  import type { BigIntFn } from "big-integer-types";
  declare export default BigIntFn
}
Run Code Online (Sandbox Code Playgroud)

这非常适用于该领域大整数,例如,对于类型检查bigInt(12).plus("144e53").哪个好.但这不包括bigInt.lcm(134, 1551),这会产生流量错误.

另一种方法是将big-integer模块的导出声明为具有某些相关功能的类型.例如:

declare module "big-integer-types" {
  declare type BigIntegerStaticMethods {
    lcm(a: BigIntInput, b: BigIntInput): BigInteger,
    /* snip */
  }

  declare type BigIntInput = number | string | BigInteger;
}

declare module "big-integer" {
  import type BigIntegerStaticMethods from "big-integer-types";
  declare export default BigIntegerStaticMethods
}
Run Code Online (Sandbox Code Playgroud)

这适用于静态方法,但我不知道怎么说可以调用 "类型" .所以我不知道如何同时实现这两个目标.

这似乎不可思议,因为与字段的功能是在JavaScript中相当普遍,流文档建议他们通过大量的努力来了JavaScript类型系统的支持去,因为它是使用.所以我认为有一个流语法来实现这一点,我只是无法弄清楚它是什么,并且无法在文档中找到它.

use*_*125 4

您可以在类中声明一个未命名的静态函数:

declare type BigIntInput = number | string | BigInteger;
declare class BigInteger {
  add(addend: BigIntInput): BigInteger;
  minus(subtractand: BigIntInput): BigInteger;

  static lcm(a: BigIntInput, b: BigIntInput): BigInteger;
  static (data?: BigIntInput): BigInteger;  
} 

BigInteger.lcm(1,2);
BigInteger(4).add(5);
Run Code Online (Sandbox Code Playgroud)