class T::Types::Union
Takes a list of types. Validates that an object matches at least one of the types.
def initialize(types)
Don't use Union.new directly, use `Private::Pool.union_of_types`
def initialize(types) @types = types.flat_map do |type| type = T::Utils.coerce(type) if type.is_a?(Union) # Simplify nested unions (mostly so `name` returns a nicer value) type.types else type end end.uniq end
def name
def name # Use the attr_reader here so we can override it in SimplePairUnion type_shortcuts(types) end
def recursively_valid?(obj)
def recursively_valid?(obj) @types.any? {|type| type.recursively_valid?(obj)} end
def subtype_of_single?(other)
def subtype_of_single?(other) "This should never be reached if you're going through `subtype_of?` (and you should be)"
def type_shortcuts(types)
def type_shortcuts(types) es.size == 1 shouldn't generally get here but it's possible if initializing the type ades Sorbet's static check and we end up on the slow path, or if someone using the T:Types::Union constructor directly (the latter possibility why we don't just move the `uniq` into `Private::Pool.union_of_types`). rn types[0].name e = T::Utils.coerce(NilClass) ass = T::Utils.coerce(TrueClass) lass = T::Utils.coerce(FalseClass) es.any? {|t| t == nilable} ining_types = types.reject {|t| t == nilable} ilable(#{type_shortcuts(remaining_types)})" types.any? {|t| t == trueclass} && types.any? {|t| t == falseclass} ining_types = types.reject {|t| t == trueclass || t == falseclass} _shortcuts([T::Private::Types::StringHolder.new("T::Boolean")] + remaining_types) s = types.map(&:name).compact.sort ny(#{names.join(', ')})"
def valid?(obj)
def valid?(obj) @types.any? {|type| type.valid?(obj)} end