我正在尝试编写一个 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
}
这适用于 大整数字段,例如用于类型检查 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);
关于javascript - 如何为具有字段的函数声明流类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47964613/
我想安装一个带有一些身份验证的私有(private)Rubygem服务器。我希望能够使用公共(public)Ubuntu服务器托管内部gem。我读到了http://docs.rubygems.org/read/chapter/18.但是那个没有身份验证-如我所见。然后我读到了https://github.com/cwninja/geminabox.但是当我使用基本身份验证(他们在他们的Wiki中有)时,它会提示从我的服务器获取源。所以。如何制作带有身份验证的私有(private)Rubygem服务器?这是不可能的吗?谢谢。编辑:Geminabox问题。我尝试“捆绑”以安装新的gem..
我刚刚为fedora安装了emacs。我想用emacs编写ruby。为ruby提供代码提示、代码完成类型功能所需的工具、扩展是什么? 最佳答案 ruby-mode已经包含在Emacs23之后的版本中。不过,它也可以通过ELPA获得。您可能感兴趣的其他一些事情是集成RVM、feature-mode(Cucumber)、rspec-mode、ruby-electric、inf-ruby、rinari(用于Rails)等。这是我当前用于Ruby开发的Emacs配置:https://github.com/citizen428/emacs
我有一个表单,其中有很多字段取自数组(而不是模型或对象)。我如何验证这些字段的存在?solve_problem_pathdo|f|%>... 最佳答案 创建一个简单的类来包装请求参数并使用ActiveModel::Validations。#definedsomewhere,atthesimplest:require'ostruct'classSolvetrue#youcouldevencheckthesolutionwithavalidatorvalidatedoerrors.add(:base,"WRONG!!!")unlesss
我想向我的Controller传递一个参数,它是一个简单的复选框,但我不知道如何在模型的form_for中引入它,这是我的观点:{:id=>'go_finance'}do|f|%>Transferirde:para:Entrada:"input",:placeholder=>"Quantofoiganho?"%>Saída:"output",:placeholder=>"Quantofoigasto?"%>Nota:我想做一个额外的复选框,但我该怎么做,模型中没有一个对象,而是一个要检查的对象,以便在Controller中创建一个ifelse,如果没有检查,请帮助我,非常感谢,谢谢
我正在使用active_admin,我在Rails3应用程序的应用程序中有一个目录管理,其中包含模型和页面的声明。时不时地我也有一个类,当那个类有一个常量时,就像这样:classFooBAR="bar"end然后,我在每个必须在我的Rails应用程序中重新加载一些代码的请求中收到此警告:/Users/pupeno/helloworld/app/admin/billing.rb:12:warning:alreadyinitializedconstantBAR知道发生了什么以及如何避免这些警告吗? 最佳答案 在纯Ruby中:classA
我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re
我可以得到Infinity和NaNn=9.0/0#=>Infinityn.class#=>Floatm=0/0.0#=>NaNm.class#=>Float但是当我想直接访问Infinity或NaN时:Infinity#=>uninitializedconstantInfinity(NameError)NaN#=>uninitializedconstantNaN(NameError)什么是Infinity和NaN?它们是对象、关键字还是其他东西? 最佳答案 您看到打印为Infinity和NaN的只是Float类的两个特殊实例的字符串
我不确定传递给方法的对象的类型是否正确。我可能会将一个字符串传递给一个只能处理整数的函数。某种运行时保证怎么样?我看不到比以下更好的选择:defsomeFixNumMangler(input)raise"wrongtype:integerrequired"unlessinput.class==FixNumother_stuffend有更好的选择吗? 最佳答案 使用Kernel#Integer在使用之前转换输入的方法。当无法以任何合理的方式将输入转换为整数时,它将引发ArgumentError。defmy_method(number)
我正在尝试用ruby中的gsub函数替换字符串中的某些单词,但有时效果很好,在某些情况下会出现此错误?这种格式有什么问题吗NoMethodError(undefinedmethod`gsub!'fornil:NilClass):模型.rbclassTest"replacethisID1",WAY=>"replacethisID2andID3",DELTA=>"replacethisID4"}end另一个模型.rbclassCheck 最佳答案 啊,我找到了!gsub!是一个非常奇怪的方法。首先,它替换了字符串,所以它实际上修改了
我知道我可以指定某些字段来使用pluck查询数据库。ids=Item.where('due_at但是我想知道,是否有一种方法可以指定我想避免从数据库查询的某些字段。某种反拔?posts=Post.where(published:true).do_not_lookup(:enormous_field) 最佳答案 Model#attribute_names应该返回列/属性数组。您可以排除其中一些并传递给pluck或select方法。像这样:posts=Post.where(published:true).select(Post.attr