dotnet / razor

Compiler and tooling experience for Razor ASP.NET Core apps in Visual Studio, Visual Studio for Mac, and VS Code.
https://asp.net
MIT License
499 stars 191 forks source link

CascadingTypeParameter not passing down nullability #10201

Open danielchalmers opened 6 months ago

danielchalmers commented 6 months ago

Discussed in https://github.com/dotnet/aspnetcore/discussions/54332

Originally posted by **danielchalmers** March 3, 2024 I have a [`MudToggleGroup>`](https://github.com/MudBlazor/MudBlazor/blob/17e9a354d741a16bebe6a5a35c21d0d5fac8f76b/src/MudBlazor/Components/Toggle/MudToggleGroup.razor#L4) but the children's types are the non-nullable [`MudToggleItem`](https://github.com/MudBlazor/MudBlazor/blob/17e9a354d741a16bebe6a5a35c21d0d5fac8f76b/src/MudBlazor/Components/Toggle/MudToggleItem.razor#L3). ```razor ``` ```razor @* MudToggleGroup.razor *@ @namespace MudBlazor @attribute [CascadingTypeParameter(nameof(T))] @inherits MudComponentBase @typeparam T ``` ```razor @* MudToggleItem.razor *@ @namespace MudBlazor @inherits MudComponentBase @typeparam T ``` There are no other ancestors with a type param named `T`. Any idea on how to pass down the nullability without manually casting them like `Value="@((int?)3)"`?
javiercn commented 6 months ago

@danielchalmers thanks for contacting us.

I don't think there's a way to do it other than anotating the T in the @typeparam which I don't remember if it's possible. This will likely require compiler work to support it.

chsienki commented 6 months ago

Looks like the type inference would need to explicitly set the type param in the presence of CascadingTypeParam


            __builder.OpenComponent<global::BlazorApp42.Components.Pages.MudToggleGroup<int?>>(5);
            __builder.AddAttribute(6, "ChildContent", (global::Microsoft.AspNetCore.Components.RenderFragment)((__builder2) => {
                global::__Blazor.BlazorApp42.Components.Pages.Home.TypeInference.CreateMudToggleItem_0<int?>(__builder2, 7, 8, // <!-- THIS. Today we don't emit <int?> so can't infer the type. We would need to explicitly add it as a type param in this case.
#nullable restore
#line (10,27)-(10,31) "C:\Projects\scratch\BlazorApp42\BlazorApp42\Components\Pages\Home.razor"
null

#line default
#line hidden
#nullable disable
                , 9, "Coffee (1)");
                __builder2.AddMarkupContent(10, "\r\n    ");
                global::__Blazor.BlazorApp42.Components.Pages.Home.TypeInference.CreateMudToggleItem_1(__builder2, 11, 12, 
#nullable restore
#line (11,27)-(11,28) "C:\Projects\scratch\BlazorApp42\BlazorApp42\Components\Pages\Home.razor"
2

#line default
#line hidden
#nullable disable
                , 13, "Tee (2)");
                __builder2.AddMarkupContent(14, "\r\n    ");
                global::__Blazor.BlazorApp42.Components.Pages.Home.TypeInference.CreateMudToggleItem_2(__builder2, 15, 16, 
#nullable restore
#line (12,27)-(12,28) "C:\Projects\scratch\BlazorApp42\BlazorApp42\Components\Pages\Home.razor"
3

#line default
#line hidden
#nullable disable
                , 17, "Water (3)");
            }
            ));
            __builder.CloseComponent();