module T::Utils::Nilable

Constants

NIL_TYPE
TypeInfo

:is_union_type, T::Boolean: whether the type is an T::Types::Union type :non_nilable_type, Class: if it is an T.nilable type, the corresponding underlying type; otherwise, nil.

Public Class Methods

get_type_info(prop_type) click to toggle source
# File lib/types/utils.rb, line 168
def self.get_type_info(prop_type)
  if prop_type.is_a?(T::Types::Union)
    non_nilable_type = T::Utils.unwrap_nilable(prop_type)
    if non_nilable_type&.is_a?(T::Types::Simple)
      non_nilable_type = non_nilable_type.raw_type
    end
    TypeInfo.new(true, non_nilable_type)
  else
    TypeInfo.new(false, nil)
  end
end
get_underlying_type(prop_type) click to toggle source

Get the underlying type inside prop_type:

- if the type is A, the function returns A
- if the type is T.nilable(A), the function returns A
# File lib/types/utils.rb, line 183
def self.get_underlying_type(prop_type)
  type_info = get_type_info(prop_type)
  if type_info.is_union_type
    type_info.non_nilable_type || prop_type
  elsif prop_type.is_a?(T::Types::Simple)
    prop_type.raw_type
  else
    prop_type
  end
end
get_underlying_type_object(prop_type) click to toggle source

The difference between this function and the above function is that the Sorbet type, like T::Types::Simple is preserved.

# File lib/types/utils.rb, line 196
def self.get_underlying_type_object(prop_type)
  T::Utils.unwrap_nilable(prop_type) || prop_type
end
is_union_with_nilclass(prop_type) click to toggle source
# File lib/types/utils.rb, line 200
def self.is_union_with_nilclass(prop_type)
  case prop_type
  when T::Types::Union
    prop_type.types.include?(NIL_TYPE)
  else
    false
  end
end