steep icon indicating copy to clipboard operation
steep copied to clipboard

`Class.new(Superclass)` cannot be assigned to a `Superclass`

Open vpellan opened this issue 3 months ago • 3 comments

When declaring a signature such as:

module Foo
	Bar: StandardError
end

This code will return the error "Cannot assign a value of type ::Class to a constant of type StandardError"

module Foo
	Bar = Class.new(StandardError)
end

This makes sense if we treat Class as any other class, but could it be possible to introduce specific behaviour in this case so that Bar is typed to StandardError, or an anonymous class that inherits from StandardError ?

vpellan avatar Oct 09 '25 16:10 vpellan

I think that could be solved with a standard type declaration like this

module Foo
  class Bar < StandardError
  end
end

another way to do it is a module/class alias

module Foo
  class Bar = StandardError
end

Strech avatar Oct 09 '25 18:10 Strech

Thanks for your suggestions ! The two suggestions give the same output:

lib/playground/playground.rb:2:2: [error] Cannot assign a value of type `::Class` to a constant of type `singleton(::Foo::Bar)`
│   ::Class <: singleton(::Foo::Bar)
│     ::Module <: singleton(::Foo::Bar)
│       ::Object <: singleton(::Foo::Bar)
│         ::BasicObject <: singleton(::Foo::Bar)
│
│ Diagnostic ID: Ruby::IncompatibleAssignment
│
└   Bar = Class.new(StandardError)
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

vpellan avatar Oct 10 '25 08:10 vpellan

I think it works on the current master. Not sure about already released versions

Strech avatar Oct 10 '25 08:10 Strech