ts2fable icon indicating copy to clipboard operation
ts2fable copied to clipboard

Why doesn't TS2Fable uncurry TypeScript function definitions?

Open OkayX6 opened this issue 4 years ago • 2 comments

Hi :)

I got adventurous and needed to create a Fable binding to the React Beautiful DnD library. Then, I got stuck for a while, when trying to create a property for my React component, and my code didn't work.

Generated F# code

// ts2fable 0.8.0
type [<AllowNullLiteral>] DragDropContextProps =
    abstract onDragEnd: result: DropResult * provided: ResponderProvided -> unit

Original TS definition

export interface DragDropContextProps {
    onDragEnd(result: DropResult, provided: ResponderProvided): void;
}

I realized that tuple arguments were passed as arrays, thanks to the generated JS code by the Feliz template project

Fixed via uncurrying

Thus, I fixed it the code by converting the prop to a curryfied property:

[<RequireQualifiedAccess>]
type DragDropContextProps =
  | OnDragEnd of (DropResult -> ResponderProvided -> unit)

Why isn't that by default, if that's the way Fable generates JS function code?

Thanks :)

OkayX6 avatar Mar 24 '21 07:03 OkayX6

I think here is that in React we want to use uncurry because of how we build the props information for React but if it was a not a React props current generation would be correct.

Unfortunately, I don't think this addressable for now in ts2fable

MangelMaxime avatar Mar 24 '21 08:03 MangelMaxime

The answer is a bit complicated because the F# compiler treats module/type methods different from local functions, but you should be able to create the props with the generated declaration for example with an object expression:

let props =
  { new DragDropContextProps with
    member _.onDragEnd(: result, provided) = doSomething() }

However the syntax is not very beautiful, especially if you have to implement multiple props. That's way we often model the component props with a union type and use keyValueList to convert them to a JS obj. However, unfortunately ts2fable cannot make this kind of conversion automatically atm.

alfonsogarciacaro avatar Mar 24 '21 15:03 alfonsogarciacaro