Fable icon indicating copy to clipboard operation
Fable copied to clipboard

[TypeScript] generic parameters are duplicated

Open goswinr opened this issue 1 year ago • 1 comments

This generates invalid typescript:

open System.Collections.Generic

type Table<'K,'V when 'K:equality> () =

    let dic = new Dictionary<'K,'V>()

    let addKeyValue key value =
        dic.[key] <- value        
   
    member _.add key value = 
        addKeyValue key value 

see REPL

The generic parameters K and V appear twice each in: export function Table$2__addKeyValue<K, V, K, V>(this$: Table$2<K, V>, key: K, value: V): void {

image

fable 4.21

goswinr avatar Sep 30 '24 14:09 goswinr

A workaround is to move internal functions of the class outside the class and inline them:

open System.Collections.Generic

let inline private addKeyValue (dic:Dictionary<'K,'V>) key value = // internal function moved here
    dic.[key] <- value  

type Table<'K,'V when 'K:equality> () =

    let dic = new Dictionary<'K,'V>()      
   
    member this.Add(key, value) = 
        addKeyValue dic key value 

goswinr avatar Oct 01 '24 07:10 goswinr