【发布时间】:2018-06-06 11:18:54
【问题描述】:
我正在尝试编写一个到处都有严格流输入的 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
}
这适用于 of 个大整数的字段,例如用于类型检查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在使用时。所以我认为有一个流语法来实现这一点,我只是无法弄清楚它是什么,也无法在文档中找到它。
【问题讨论】:
标签: javascript flowtype flow-typed