···6363 end
64646565 def self.subtype?(type, of:)
6666- type = type.block.call if Types::DeferredType === type
6666+ supertype = of
6767+ subtype = type
67686868- (of == type) || case of
6969+ subtype = subtype.block.call if Types::DeferredType === subtype
7070+7171+ return true if supertype == subtype
7272+7373+ case supertype
6974 when Literal::Type
7070- of >= type
7575+ supertype >= subtype
7176 when Module
7272- (Module === type) ? of >= type : false
7777+ case subtype
7878+ when Module
7979+ supertype >= subtype
8080+ when Numeric
8181+ Numeric >= supertype
8282+ when String
8383+ String >= supertype
8484+ when Symbol
8585+ Symbol >= supertype
8686+ when ::Array
8787+ ::Array >= supertype
8888+ when ::Hash
8989+ ::Hash >= supertype
9090+ else
9191+ false
9292+ end
7393 when Range
7474- of.cover?(type)
9494+ supertype.cover?(subtype)
7595 else
7696 false
7797 end
+21-12
lib/literal/types/union_type.rb
···4848 end
49495050 def each(&)
5151+ @primitives.each(&)
5152 @types.each(&)
5253 end
53545455 def deconstruct
5555- @types.to_a
5656+ to_a
5657 end
57585859 def [](key)
5959- if @types.include?(key)
6060+ if @primitives.include?(key) || @types.include?(key)
6061 key
6161- else
6262- raise ArgumentError.new("#{key} not in #{inspect}")
6362 end
6463 end
65646565+ def fetch(key)
6666+ self[key] or raise KeyError.new("Key not found: #{key.inspect}")
6767+ end
6868+6669 def record_literal_type_errors(ctx)
6767- @types.each do |type|
7070+ each do |type|
6871 ctx.add_child(label: type.inspect, expected: type, actual: ctx.actual)
6972 end
7373+7074 ctx.children.clear if ctx.children.none? { |c| c.children.any? }
7175 end
72767377 def >=(other)
7878+ types = @types
7979+ primitives = @primitives
8080+7481 case other
7582 when Literal::Types::UnionType
7676- other.types.all? do |other_type|
7777- @types.any? do |type|
7878- Literal.subtype?(type, of: other_type)
7979- end
8383+ types_have_at_least_one_subtype = other.types.all? do |other_type|
8484+ primitives.any? { |p| Literal.subtype?(p, of: other_type) } || types.any? { |t| Literal.subtype?(t, of: other_type) }
8085 end
8181- else
8282- @types.any? do |type|
8383- Literal.subtype?(other, of: type)
8686+8787+ primitives_have_at_least_one_subtype = other.primitives.all? do |other_primitive|
8888+ primitives.any? { |p| Literal.subtype?(p, of: other_primitive) } || types.any? { |t| Literal.subtype?(t, of: other_primitive) }
8489 end
9090+9191+ types_have_at_least_one_subtype && primitives_have_at_least_one_subtype
9292+ else
9393+ types.any? { |t| Literal.subtype?(other, of: t) } || primitives.any? { |p| Literal.subtype?(other, of: p) }
8594 end
8695 end
8796