Support union-typed templated component parameters
- Dominant language
- C#
- Stars
- 20.7k
- Forks
- 4.3k
- PR merge metrics
- PR metrics pending
Description
### Background
C# union types are new in .NET 11, and a Blazor component parameter can be typed as a union. For example, `SlotContent` accepts either plain text or a Razor template:
```csharp
using Microsoft.AspNetCore.Components;
public union SlotContent(string, RenderFragment);
```
A templated component can expose a parameter of that union type:
```razor
@code {
[Parameter, EditorRequired]
public SlotContent Content { get; set; }
}
```
The `RenderFragment` case can currently be assigned directly:
```razor
```
Razor normally allows a component parameter of type `RenderFragment` or `RenderFragment` to be supplied using a child element whose name matches the template parameter:
```razor
@currentCount
```
This is the same templated-component syntax used for parameters such as `ItemTemplate`, `Authorized`, and `LoggingIn`.
### Problem
The named `` template doesn't work when the declared parameter type is a union containing a `RenderFragment` case.
Razor only recognizes the element as a template parameter when the parameter itself is typed as `RenderFragment` or `RenderFragment`. It doesn't recognize a union containing one of those types, so the generated fragment isn't assigned through the union's implicit conversion.
This issue concerns named template parameters. It isn't about the conventional `ChildContent` parameter used for direct, unwrapped component content.
### Request
Support named template elements for union-typed component parameters that include a compatible `RenderFragment` or `RenderFragment` case.
For example, Razor should compile:
```razor
@currentCount
```
by generating a `RenderFragment` for the `` element and assigning it through the union's implicit conversion.
If this isn't feasible, provide a clear compiler diagnostic that explains that the union's fragment case must be assigned directly.
### Related work
- String-literal attributes for unions containing a `string` case: dotnet/razor#13188
- ASP.NET Core union support: dotnet/aspnetcore#64599
- Blazor union verification: dotnet/aspnetcore#67296
- Union specification: aspnet/specs#782
Contributor guide
Research direction
Start with Razor's templated-component parameter recognition and compare the related union-support issues dotnet/razor#13188, dotnet/aspnetcore#64599, dotnet/aspnetcore#67296, and aspnet/specs#782. Done means the named example compiles by converting its generated RenderFragment through the union, or emits a clear diagnostic if unsupported.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100