@@ -59,7 +59,7 @@ let traslate_declaration_kind ~config ~loc ~output_file_relative ~resolver
5959 | false -> None
6060 (* one means don't know *)
6161 in
62- let import_string_opt, name_as =
62+ let import_string_opt, name_as, import_exact_opt, remote_export_name_opt =
6363 type_attributes |> Annotation. get_attribute_import_renaming
6464 in
6565 let unboxed_annotation =
@@ -83,7 +83,7 @@ let traslate_declaration_kind ~config ~loc ~output_file_relative ~resolver
8383 |> Translation. translate_dependencies ~config ~output_file_relative
8484 ~resolver
8585 in
86- {CodeItem. import_types; export_from_type_declaration}
86+ {CodeItem. import_types; export_from_type_declaration; expected_type = None }
8787 in
8888 let translate_label_declarations ?(inline = false ) label_declarations =
8989 let field_translations =
@@ -146,11 +146,15 @@ let traslate_declaration_kind ~config ~loc ~output_file_relative ~resolver
146146 let name_with_module_path =
147147 typeName_ |> TypeEnv. add_module_path ~type_env |> ResolvedName. to_string
148148 in
149- let type_name, as_type_name =
150- match name_as with
151- | Some as_string -> (as_string, " $$" ^ name_with_module_path)
152- | None -> (name_with_module_path, " $$" ^ name_with_module_path)
149+ (* Use the remote export name (if provided) to build the import and alias.
150+ Preserve casing from the TS source exactly. *)
151+ let remote_type_name =
152+ match remote_export_name_opt with
153+ | Some s -> s
154+ | None -> name_with_module_path
153155 in
156+ let type_name = remote_type_name in
157+ let as_type_name = remote_type_name ^ " $TypeScript" in
154158 let import_path = import_string |> ImportPath. from_string_unsafe in
155159 let base_import =
156160 {CodeItem. type_name; as_type_name = Some as_type_name; import_path}
@@ -167,6 +171,59 @@ let traslate_declaration_kind ~config ~loc ~output_file_relative ~resolver
167171 (type_expr
168172 |> TranslateTypeExprFromTypes. translate_type_expr_from_types ~config
169173 ~type_env )
174+ | RecordDeclarationFromTypes label_declarations ->
175+ Some (label_declarations |> translate_label_declarations)
176+ | VariantDeclarationFromTypes constructor_declarations ->
177+ let variants =
178+ constructor_declarations
179+ |> List. map (fun constructor_declaration ->
180+ let constructor_args = constructor_declaration.Types. cd_args in
181+ let attributes = constructor_declaration.cd_attributes in
182+ let name = constructor_declaration.cd_id |> Ident. name in
183+ let args_translation =
184+ match constructor_args with
185+ | Cstr_tuple type_exprs ->
186+ type_exprs
187+ |> TranslateTypeExprFromTypes
188+ .translate_type_exprs_from_types ~config ~type_env
189+ | Cstr_record label_declarations ->
190+ [
191+ label_declarations
192+ |> translate_label_declarations ~inline: true ;
193+ ]
194+ in
195+ let arg_types =
196+ args_translation
197+ |> List. map (fun {TranslateTypeExprFromTypes. type_} -> type_)
198+ in
199+ (name, attributes, arg_types))
200+ in
201+ let variants_no_payload, variants_with_payload =
202+ variants |> List. partition (fun (_ , _ , arg_types ) -> arg_types = [] )
203+ in
204+ let no_payloads =
205+ variants_no_payload
206+ |> List. map (fun (name , attributes , _argTypes ) ->
207+ (name, attributes) |> create_case ~poly: false )
208+ in
209+ let payloads =
210+ variants_with_payload
211+ |> List. map (fun (name , attributes , arg_types ) ->
212+ let type_ =
213+ match arg_types with
214+ | [type_] -> type_
215+ | _ -> Tuple arg_types
216+ in
217+ {
218+ case = (name, attributes) |> create_case ~poly: false ;
219+ t = type_;
220+ })
221+ in
222+ let variant_typ =
223+ create_variant ~inherits: [] ~no_payloads ~payloads ~polymorphic: false
224+ ~tag: tag_annotation ~unboxed: unboxed_annotation
225+ in
226+ Some {TranslateTypeExprFromTypes. dependencies = [] ; type_ = variant_typ}
170227 | _ -> None
171228 in
172229
@@ -197,24 +254,46 @@ let traslate_declaration_kind ~config ~loc ~output_file_relative ~resolver
197254 in
198255 let export_type_body =
199256 match expected_translation_opt with
200- | Some tr ->
201- (* $GenTypeImport<Expected, Imported> *)
202- ident GentypeImportHelper. name ~type_args: [tr.type_; imported_ident]
257+ | Some tr -> (
258+ let expected_for_wrapper =
259+ if GentypeImportHelper. should_inline_expected tr.type_ then tr.type_
260+ else
261+ name_with_module_path ^ " $ReScript"
262+ |> ident ~builtin: false
263+ ~type_args: (type_vars |> List. map (fun s -> TypeVar s))
264+ in
265+ match import_exact_opt with
266+ | Some true ->
267+ ident GentypeImportHelper. strict_name
268+ ~type_args: [imported_ident; expected_for_wrapper]
269+ | _ ->
270+ ident GentypeImportHelper. name
271+ ~type_args: [expected_for_wrapper; imported_ident])
203272 | None -> imported_ident
204273 in
205274 typeName_
206275 |> create_export_type_from_type_declaration ~doc_string
207276 ~annotation: GenType ~loc ~name_as: None ~opaque: (Some false )
208277 ~type_: export_type_body ~type_env ~type_vars
209278 in
210- [{CodeItem. import_types; export_from_type_declaration}]
279+ [
280+ {
281+ CodeItem. import_types;
282+ export_from_type_declaration;
283+ expected_type =
284+ (match expected_translation_opt with
285+ | Some tr -> Some tr.type_
286+ | None -> None );
287+ };
288+ ]
211289 | (GeneralDeclarationFromTypes None | GeneralDeclaration None ), None ->
212290 {
213291 CodeItem. import_types = [] ;
214292 export_from_type_declaration =
215293 type_name
216294 |> create_export_type_from_type_declaration ~doc_string ~annotation ~loc
217295 ~name_as ~opaque: (Some true ) ~type_: unknown ~type_env ~type_vars ;
296+ expected_type = None ;
218297 }
219298 |> return_type_declaration
220299 | GeneralDeclarationFromTypes (Some type_expr ), None ->
@@ -270,6 +349,7 @@ let traslate_declaration_kind ~config ~loc ~output_file_relative ~resolver
270349 type_name
271350 |> create_export_type_from_type_declaration ~doc_string ~annotation ~loc
272351 ~name_as ~opaque ~type_ ~type_env ~type_vars ;
352+ expected_type = None ;
273353 }
274354 |> return_type_declaration
275355 | VariantDeclarationFromTypes constructor_declarations , None ->
@@ -348,7 +428,7 @@ let traslate_declaration_kind ~config ~loc ~output_file_relative ~resolver
348428 |> List. map (fun (_ , _ , _ , import_types ) -> import_types)
349429 |> List. concat
350430 in
351- {CodeItem. export_from_type_declaration; import_types}
431+ {CodeItem. export_from_type_declaration; import_types; expected_type = None }
352432 |> return_type_declaration
353433 | NoDeclaration , None -> []
354434
0 commit comments