ts2fable icon indicating copy to clipboard operation
ts2fable copied to clipboard

Type aliases for single element tuples are not translated correctly

Open grantneale opened this issue 6 years ago • 3 comments

Type aliases for single element tuples are translated incorrectly.

index.d.ts

type Tuple1 = [string]

type Tuple2 = [string, bool]

// ...

Actual

// ts2fable 0.6.2
module rec moduleName
open System
open Fable.Core
open Fable.Import.JS

// This is INCORRECT, but happens to sort of work for strings.  For other types, it doesn't work as intended.
type Tuple1 =
    string

// Correct
type Tuple2 =
    string * bool

Expected

// ts2fable 0.6.2
module rec moduleName
open System
open Fable.Core
open Fable.Import.JS

// Not sure what the correct behaviour is, as single element tuples aren't possible in F#
// Perhaps this?
type Tuple1 = string * unit

type Tuple2 =
    string * bool

grantneale avatar Mar 16 '19 00:03 grantneale

My intital description wasn't quite correct, updated above

grantneale avatar Mar 16 '19 02:03 grantneale

To further clarify. The F# equivalent of a single-value tuple should be a scalar, ideally. However, the JS representation of a single-value tuple from Typescript is an array. So either:

  • fable-compiler needs to flatten arrays to scalars in such cases; or
  • ts2fable needs to apply some sort of workaround.

grantneale avatar Mar 16 '19 04:03 grantneale

Seems like tuple types are translated by concatenating the type strings with "*"

https://github.com/fable-compiler/ts2fable/blob/master/src/print.fs#L38

However

- let oneTuple = System.Tuple.Create(1);;                                   
val oneTuple : System.Tuple<int> = (1)

> let twoTuple = System.Tuple.Create(1,2);;
val twoTuple : int * int = (1, 2)

It's not possible to do something like let (x) = oneTuple to de-structure the one member either.

As shown in

https://github.com/fsharp/fsharp/blob/master/tests/fsharp/typecheck/sigs/pos27.fs#L4

it seems like System.Tuple<'a, 'b, 'c...> would be the most correct typing to employ here.

resolritter avatar Mar 29 '19 05:03 resolritter