Skip to content

Rails model enum types are too generic #1700

@fazo96

Description

@fazo96

While adopting tapioca on our project I noticed two issues with rails model enums:

  1. Looks like our enums are typed as T.nilable(::String) even though the actual possible values for the enum should be available by tapioca to generate
  2. Even if the enums were to be typed correctly, there is no exposure of the type alias to be able to use it in sorbet type signatures in the application code (not even sure if this can be done and to what extent, I am new to sorbet and tapioca)

The code generating the types for the enums seems to be this:

sig { params(enum_map: T::Hash[T.untyped, T.untyped]).returns(String) }
def type_for_enum(enum_map)
value_type = enum_map.values.map { |v| v.class.name }.uniq
value_type = if value_type.length == 1
value_type.first
else
"T.any(#{value_type.join(", ")})"
end
"T::Hash[T.any(String, Symbol), #{value_type}]"
end

I wonder if it's possible to solve both problems. The main method to get the enum value seems to be generated by some other Compiler though so it might be tricky

I might give it a shot to implement this myself, recommendations and suggestions are welcome

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions