Skip to content

Commit

Permalink
fix: forward generics to the generated types when transforming a `Typ…
Browse files Browse the repository at this point in the history
…eLiteral`

[converter]

=== changelog ===
```ts
export type Callback<Param, AtomType> = (event: {
    atom: AtomType;
}) => void;
```

```fs
[<AllowNullLiteral>]
[<Interface>]
type Callback<'Param, 'AtomType> =
    [<Emit("$0($1...)")>]
    abstract member Invoke: event: Callback.event<'AtomType> -> unit

module Callback =

    [<Global>]
    [<AllowNullLiteral>]
    type event<'AtomType>
        [<ParamObject; Emit("$0")>]
        (
            atom: 'AtomType
        ) =

        member val atom : 'AtomType = nativeOnly with get, set
```

Fix #148
  • Loading branch information
MangelMaxime committed Dec 16, 2024
1 parent eecbbc6 commit e0039f4
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 2 deletions.
43 changes: 41 additions & 2 deletions src/Glutinum.Converter/Transform.fs
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,23 @@ let rec private transformType (context: TransformContext) (glueType: GlueType) :
| GlueMember.ConstructSignature _ -> true
)

let typeParameterNames =
typeLiteralInfo.Members
|> List.choose (fun m ->
match m with
| GlueMember.MethodSignature { Type = typ }
| GlueMember.Method { Type = typ }
| GlueMember.Property { Type = typ }
| GlueMember.GetAccessor { Type = typ }
| GlueMember.SetAccessor { ArgumentType = typ }
| GlueMember.CallSignature { Type = typ }
| GlueMember.IndexSignature { Type = typ }
| GlueMember.ConstructSignature { Type = typ } ->
match typ with
| GlueType.TypeParameter name -> Some name
| _ -> None
)

if hasNoIndexSignature then

let typeLiteralParameters =
Expand Down Expand Up @@ -720,6 +737,19 @@ let rec private transformType (context: TransformContext) (glueType: GlueType) :
: FSharpExplicitField
)

let typeParameters =
typeParameterNames
|> List.map (fun name ->
({
Name = name
Constraint = None
Default = None
}
: FSharpTypeParameterInfo)
|> FSharpTypeParameter.FSharpTypeParameter

)

({
Attributes = [ FSharpAttribute.Global; FSharpAttribute.AllowNullLiteral ]
Name = context.CurrentScopeName
Expand All @@ -731,7 +761,7 @@ let rec private transformType (context: TransformContext) (glueType: GlueType) :
}
SecondaryConstructors = []
ExplicitFields = explicitFields
TypeParameters = []
TypeParameters = typeParameters
}
: FSharpClass)
|> FSharpType.Class
Expand Down Expand Up @@ -759,7 +789,16 @@ let rec private transformType (context: TransformContext) (glueType: GlueType) :
({
Name = context.FullName
FullName = context.FullName
TypeArguments = []
TypeArguments =
typeParameterNames
|> List.map (fun name ->
({
Name = $"'%s{name}"
TypeParameters = []
}
: FSharpMapped)
|> FSharpType.Mapped
)
Type = FSharpType.Discard
}
: FSharpTypeReference)
Expand Down
5 changes: 5 additions & 0 deletions tests/specs/references/typeLiteral/functionType/generics.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export type Callback<Param, AtomType> = (event: {
type: 'CREATE' | 'REMOVE';
param: Param;
atom: AtomType;
}) => void;
40 changes: 40 additions & 0 deletions tests/specs/references/typeLiteral/functionType/generics.fsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
module rec Glutinum

open Fable.Core
open Fable.Core.JsInterop
open System

[<AllowNullLiteral>]
[<Interface>]
type Callback<'Param, 'AtomType> =
[<Emit("$0($1...)")>]
abstract member Invoke: event: Callback.event<'Param, 'AtomType> -> unit

module Callback =

[<Global>]
[<AllowNullLiteral>]
type event<'Param, 'AtomType>
[<ParamObject; Emit("$0")>]
(
``type``: Callback.event.``type``,
param: 'Param,
atom: 'AtomType
) =

member val ``type`` : Callback.event.``type`` = nativeOnly with get, set
member val param : 'Param = nativeOnly with get, set
member val atom : 'AtomType = nativeOnly with get, set

module event =

[<RequireQualifiedAccess>]
[<StringEnum(CaseRules.None)>]
type ``type`` =
| CREATE
| REMOVE

(***)
#r "nuget: Fable.Core"
#r "nuget: Glutinum.Types"
(***)

0 comments on commit e0039f4

Please sign in to comment.