0

我的一个课程依赖于 gem Geokit,它不提供自己的 RBI 文件,也不包含在sorbet-typedrepo 中。我自己为它手写了几个 RBI 文件,包括我在自己的代码中使用的方法的签名。

当我尝试将依赖 Geokit 的类更改为 时typed: true,它抱怨我使用的方法不存在。

类类型检查在typed: false.

geokit.rbi

# typed: strong

module Geokit
end

bounds.rbi

# typed: strong

class Geokit::Bounds
    sig do
        params(
            thing: T.any(Geokit::Bounds, T::Array[T.any(T::Array[Numeric], Numeric, String)], String, Geokit::LatLng),
            other: T.nilable(T.any(T::Array[Numeric], String, Geokit::LatLng))
        ).returns(Geokit::Bounds)
    end
    def normalize(thing, other = nil); end
end

库/平台/x.rb

class X
  BOUNDS = Geokit::Bounds.normalize([[0.8852118e2, -0.751305e1], [0.689324e2, -0.386637e1]])
end

我得到的错误如下:

lib/platform/x.rb:2: Method normalize does not exist on T.class_of(Geokit::Bounds) https://srb.help/7003
     2 |  BOUNDS = Geokit::Bounds.normalize([[0.8852118e2, -0.751305e1], [0.689324e2, -0.386637e1]])
                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  Autocorrect: Use `-a` to autocorrect
    lib/platform/x.rb:2: Replace with initialize
     2 |  BOUNDS = Geokit::Bounds.normalize([[0.8852118e2, -0.751305e1], [0.689324e2, -0.386637e1]])
4

1 回答 1

1

self.该方法的 RBI 定义中缺少您。Sorbet 认为这normalize是一个实例方法Bounds

# typed: strong

class Geokit::Bounds
    sig do
        params(
            thing: T.any(Geokit::Bounds, T::Array[T.any(T::Array[Numeric], Numeric, String)], String, Geokit::LatLng),
            other: T.nilable(T.any(T::Array[Numeric], String, Geokit::LatLng))
        ).returns(Geokit::Bounds)
    end
    def self.normalize(thing, other = nil); end
end
于 2019-10-03T23:15:03.303 回答