From 58386b217cf02345767ff9267215bb0cb2ef23fa Mon Sep 17 00:00:00 2001 From: Kevin F Date: Thu, 13 Feb 2025 10:36:06 +0100 Subject: [PATCH] Improve type support for ts #645 --- Feliz.CompilerPlugins/AstUtils.fs | 6 +++--- Feliz.CompilerPlugins/ReactComponent.fs | 8 +++++++- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/Feliz.CompilerPlugins/AstUtils.fs b/Feliz.CompilerPlugins/AstUtils.fs index 7d08f669..61aa0476 100644 --- a/Feliz.CompilerPlugins/AstUtils.fs +++ b/Feliz.CompilerPlugins/AstUtils.fs @@ -9,9 +9,9 @@ open System.Text.RegularExpressions let cleanFullDisplayName str = Regex.Replace(str, @"`\d+", "").Replace(".", "_") -let makeIdent name: Fable.Ident = +let makeIdent _type name: Fable.Ident = { Name = name - Type = Fable.Any + Type = _type IsCompilerGenerated = true IsThisArgument = false IsMutable = false @@ -22,7 +22,7 @@ let makeUniqueIdent (name: string) = if i < 0 then "Z" + (abs i).ToString("X") else i.ToString("X") - "$" + name + (Guid.NewGuid().GetHashCode() |> hashToString) |> makeIdent + "$" + name + (Guid.NewGuid().GetHashCode() |> hashToString) |> makeIdent Fable.Any let makeValue r value = Fable.Value(value, r) diff --git a/Feliz.CompilerPlugins/ReactComponent.fs b/Feliz.CompilerPlugins/ReactComponent.fs index 4ad4675b..c956cbe0 100644 --- a/Feliz.CompilerPlugins/ReactComponent.fs +++ b/Feliz.CompilerPlugins/ReactComponent.fs @@ -194,7 +194,13 @@ type ReactComponentAttribute(?exportDefault: bool, ?import: string, ?from:string else // rewrite all other arguments into getters of a single props object // TODO: transform any callback into into useCallback(callback) to stabilize reference - let propsArg = AstUtils.makeIdent (sprintf "%sInputProps" (AstUtils.camelCase decl.Name)) + let propsArg = + let type_ = + let fieldNames, genericArgs = decl.Args |> List.map (fun arg -> arg.DisplayName, arg.Type) |> List.unzip + Fable.Type.AnonymousRecordType(Array.ofList fieldNames, genericArgs, false) + let name = sprintf "%sInputProps" (AstUtils.camelCase decl.Name) + AstUtils.makeIdent type_ name + let propBindings = ([], decl.Args) ||> List.fold (fun bindings arg -> let getterKey = if arg.DisplayName = "key" then "$key" else arg.DisplayName