Exporting POJOs for consumption in JS project / "Emit" attribute query #2937
-
Hoping someone might be able to help with a very basic JS interop query. I have the following file: // user.dto.fs
module Types
type UserDto = {
id: int
firstName: string
lastName: string
} I want to export this and other DTOs for consumption in a JS project. So I believe I want an import { UserDto } from '../out-prod/user.dto.js';
export { UserDto }; My attempt looks like this (it took me an embarrassingly long time to work this out): // index.fs
module Index
open Fable.Core
[<Import("UserDto", "../out-prod/user.dto.js")>]
[<Emit("export { $0 }")>]
let UserDto () = jsNative
UserDto() This works, but I feel like I'm probably over-complicating things. What's the correct way to do this? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hmm, this is tricky because there's no mechanism in F# to re-export types (only type aliases but these are erased in the generated code). In this case, I'd probably just write the index.js file directly in JS, unless there's a lot of other code that you want to write directly in F#. If you still need to write it in F# maybe you can use import expressions instead of attributes, which also generate a export if the member is public. Also, you can use the open Fable.Core.JsInterop
let UserDto: JsConstructor = importMember "${outDir}/user.dto.js" |
Beta Was this translation helpful? Give feedback.
Hmm, this is tricky because there's no mechanism in F# to re-export types (only type aliases but these are erased in the generated code). In this case, I'd probably just write the index.js file directly in JS, unless there's a lot of other code that you want to write directly in F#. If you still need to write it in F# maybe you can use import expressions instead of attributes, which also generate a export if the member is public. Also, you can use the
${outDir}
macro so you don't need to type the output directory manually in the import. Something like this should work: