fable-compiler/ts2fable

Instead of reducing optional generic type parameter, required type parameter gets reduced

Closed this issue · 0 comments

Generic type parameter with defaults (optional type parameter) in typescript, are emulated via type aliases in F#. But instead of reducing the optional type parameters, the required parameters are reduced:

type A<T, U = {}> = {}

gets converted into

type A<'U> =
    A<obj, 'U>

type [<AllowNullLiteral>] A<'T, 'U> =
    interface end

but correct is:

type A<'T> =
    A<'T, obj>

type [<AllowNullLiteral>] A<'T, 'U> =
    interface end

-> instead of keeping 'T and applying 'U, it's the other way around: 'T gets fixed, while 'U is kept.

Even worse with more parameters:

type B<S, T, U = {}, V = {}> = {}
type B<'T, 'U, 'V> =
    B<obj, 'T, 'U, 'V>

type B<'U, 'V> =
    B<obj, obj, 'U, 'V>

type [<AllowNullLiteral>] B<'S, 'T, 'U, 'V> =
    interface end

correct is:

type B<'S, 'T> =
    B<'S, 'T, obj, obj>

type B<'S, 'T, 'U> =
    B<'S, 'T, 'U, obj>

type [<AllowNullLiteral>] B<'S, 'T, 'U, 'V> =
    interface end

-> instead of fixating generic parameters from end to front, it's the opposite.
(optional parameter must occur after all required parameters)

Note: I'm currently working on this