繁体   English   中英

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

[英]How to declare Flow types for a function which has fields?

我正在尝试编写一个Javascript项目,其中包含严格的流式输入。 我也依赖于big-integer flow-typed中没有预设的流注释,谷歌没有提供有关该主题的任何有用的东西。

像许多JavaScript包一样, big-integer导出一个函数,通常称为bigInt 这可以直接调用,如下所示: bigInt("134e134") 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
}

这非常适用于大整数领域,例如用于类型检查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
}

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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