-
Notifications
You must be signed in to change notification settings - Fork 156
Closed
Description
While adopting tapioca on our project I noticed two issues with rails model enums:
- 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 - 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:
tapioca/lib/tapioca/dsl/compilers/active_record_enum.rb
Lines 94 to 104 in d2a27bd
| 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
Labels
No labels