如果我定义一个可枚举的类
# typed: strict
class MyEnumerable
extend T::Generic
extend T::Sig
include Enumerable
Elem = type_member
sig {override.params(blk: T.proc.params(arg: Elem).returns(BasicObject)).returns(NilClass)}
def each(&blk)
end
end
这种类型检查srb tc
得很好,但如果我尝试运行srb rbi update
,我会得到一个错误。检查reflection.json.err
reflection.rbi:15619: Type variable `Elem` needs to be declared as `= type_member(SOMETHING)` https://srb.help/5018
15619 | Elem = ::T.let(nil, ::T.untyped)
^^^^
将声明更改Elem
为(即如何在Enumerable
rbi中定义)
Elem = type_member(:out)
现在srb tc
给了我错误
Classes can only have invariant type members https://srb.help/5016
9 | Elem = type_member(:out)
^^^^^^^^^^^^^^^^^^^^^^^^
Errors: 1
将Elem
声明更改为
Elem = type_member(:invariant)
现在srb tc
给了我错误
Invalid variance kind, only :out and :in are supported https://srb.help/4011
9 | Elem = type_member(:invariant)
^^^^^^^^^^
Errors: 1
如何Enumerable
使用 sorbet 在项目中实现一个类,以便它在运行时进行类型检查并且不会出错srb rbi update
?