@@ -601,7 +601,7 @@ let propagate_annotation_to_sub_types ~code_items
601601 in
602602 (new_type_map, ! annotated_set)
603603
604- let emit_translation_as_string ~config ~file_name
604+ let emit_translation_as_string ~( config : Config.t ) ~file_name
605605 ~input_cmt_translate_type_declarations ~output_file_relative ~resolver
606606 (translation : Translation.t ) =
607607 let initial_env =
@@ -635,6 +635,17 @@ let emit_translation_as_string ~config ~file_name
635635 |> List. map (fun (type_declaration : CodeItem.type_declaration ) ->
636636 type_declaration.export_from_type_declaration)
637637 in
638+ (* Determine if we need to emit the helper alias for $GenTypeImport. *)
639+ let needs_gentype_import_helper =
640+ export_from_type_declarations
641+ |> List. exists
642+ (fun
643+ ({CodeItem. export_type} : CodeItem.export_from_type_declaration ) ->
644+ match export_type.type_ with
645+ | Ident {name; _} when String. equal name GentypeImportHelper. name ->
646+ true
647+ | _ -> false )
648+ in
638649 let type_name_is_interface ~env =
639650 type_name_is_interface ~export_type_map
640651 ~export_type_map_from_other_files: env.export_type_map_from_other_files
@@ -648,8 +659,33 @@ let emit_translation_as_string ~config ~file_name
648659 and env = initial_env in
649660 let env, emitters =
650661 (* imports from type declarations go first to build up type tables *)
651- import_types_from_type_declarations @ translation.import_types
652- |> List. sort_uniq Translation. import_type_compare
662+ let all_import_types =
663+ import_types_from_type_declarations @ translation.import_types
664+ |> List. sort_uniq Translation. import_type_compare
665+ in
666+ (* Prefer direct imports of an alias name over aliased imports from other modules.
667+ Use a single pass map: keep first direct (no alias) for a given name,
668+ otherwise keep the first seen. *)
669+ let chosen_by_name =
670+ List. fold_left
671+ (fun acc (it : CodeItem.import_type ) ->
672+ let key =
673+ match it.as_type_name with
674+ | Some alias -> alias
675+ | None -> it.type_name
676+ in
677+ match StringMap. find key acc with
678+ | (prev : CodeItem.import_type ) -> (
679+ match (prev.as_type_name, it.as_type_name) with
680+ | None , Some _ -> acc (* keep direct over aliased *)
681+ | _ -> acc)
682+ | exception Not_found -> StringMap. add key it acc)
683+ StringMap. empty all_import_types
684+ in
685+ let filtered_import_types =
686+ chosen_by_name |> StringMap. to_seq |> Seq. map snd |> List. of_seq
687+ in
688+ filtered_import_types
653689 |> emit_import_types ~config ~emitters ~env
654690 ~input_cmt_translate_type_declarations ~output_file_relative ~resolver
655691 ~type_name_is_interface
@@ -702,6 +738,12 @@ let emit_translation_as_string ~config ~file_name
702738 module_items_emitter
703739 |> ExportModule. emit_all_module_items ~config ~emitters ~file_name
704740 in
741+ (* If we used the $GenTypeImport wrapper, emit its helper alias early. *)
742+ let emitters =
743+ if needs_gentype_import_helper then
744+ Emitters. export_early ~emitters GentypeImportHelper. alias
745+ else emitters
746+ in
705747 emitters
706748 |> emit_requires ~imported_value_or_component: false ~early: true ~config
707749 ~requires: final_env.requires_early
0 commit comments