如何指定String
泛型类型?
# typed: strict
class NameList < Array
extend T::Sig
sig {params(names: T::Array[String]).void}
def initialize(names)
names.each { |n| self << n}
end
sig {returns(String)}
def csv
(map { |n| n.join(',') }).join("\n")
end
end
nl = NameList.new(['Bruce', 'Clark', 'Peter'])
# Output length of first name.
puts(nl.first&.size) # String#size
puts(nl.first&.sizee) # misspelled size - should be an error
# Sorbet-Static output:
# editor.rb:4: Type Elem declared by parent Array must be re-declared in NameList https://srb.help/5036
# 4 |class NameList < Array
# ^^^^^^^^^^^^^^^^^^^^^^
# https://github.com/sorbet/sorbet/tree/master/rbi/core/array.rbi#L350: Elem declared in parent here
# 350 | Elem = type_member(:out)
# ^^^^^^^^^^^^^^^^^^^^^^^^
# Errors: 1