ruby / rbs

Type Signature for Ruby
Other
1.94k stars 211 forks source link

Array(T | Array[T]) not well handled #1874

Open HoneyryderChuck opened 3 months ago

HoneyryderChuck commented 3 months ago

Given a variable typed as smth or collection of smth, Array(a) does not spread correctly:


# a: Symbol | Array[Symbol]
Array(a).each do |el|
# el is typed as Symbol | Array[Symbol], instead of Symbol
ParadoxV5 commented 3 months ago

The problem is once again https://github.com/ruby/rbs/blob/45b34bd9bf9a2b6872ae832f6a344b23dc75ec3d/core/kernel.rbs#L442-L444 Contray to our expectation, former overloads does not narrow latter overloads.

We’d have to change that to something like [T] (T ele) -> [T] | Array[untyped], yet this still won’t help with the topic without more efforts done by the type checkers.

soutaro commented 3 months ago

Confirmed. The type of Array(a) is Array[Symbol | Array[Symbol]] where it is expected to be Array[Symbol]. Not sure if I can fix the problem... 😫