This is in regards to the new @typeParam released in 0.6.0 version.
I am not sure if Type Constraints can be defined on the generic type passed to @typeParam.
If this is something which already existing, can you please direct me to some semantics help. And if it is not there; is there any plan to support this.
This feature will be very much helpful while developing abstract components which must take "T" of some base type for example.
This is something I'd like to do eventually, but it's not really straightforward to do right now.
If you need a workaround for this, you could create a base class in C# and then inherit it with @inherits
Actually, nevermind that won't work. You'd need to declare the type parameters on the derived class as well.
Yeh.. I have already tried inheriting from base class, it didn't work for the very reason you have mentioned.
Right now I am progressing with keeping "T" to be of "anytype".
BTW, Templated Components feature is awesome. Loving it. Thank you for all your efforts.
I don't know if what's been said already includes this but I'm currently sitting infront of the problem that after I had to mark my generic parameters as classes using where T : class
, I would now have to ensure the same for @typeparam
Same here, also searching for a way to add generic type constraints via @typeparam
@rynowak Will this issue be mitigated by #5487 ? I.e. we can just put the generic constraints in the partial class?
Just hit this myself, reduces the value proposition of templated components significantly with this limitation at present.
Another vote. I agree value is limited without this...I鈥檇 like to allow templated components to render markup that鈥檚 common to a family of ViewModels by using properties defined in their base type, but without generic type constraints am unable to do this cleanly. This shortcoming wouldn't stand out as much if the rest of Blazor wasn't so damn sexy...appreciate what you guys are doing! =)
@srowan answer appears to be yes, but only after an explicit build (not at design time). The .razor.g.cs
files don't have the constraint.
Given these two files:
@typeparam TItem
@code {
[Parameter]
public TItem Foo { get; set; }
}
public partial class TestComponent<TItem> where TItem : struct
{
}
The following will compile: <TestComponent Foo="1" TItem="int" />
But once you change the constraint to where TItem : class
and do a full build, it won't.
CS0452 The type 'int' must be a reference type in order to use it as parameter 'TItem' in the generic type or method 'TestComponent
'
So, @OkieCoder1, this may be a feasible workaround for now. You really only need a do-nothing partial class that contains the constraint, and to be aware that at design time, changes to the constraint won't properly be considered.
We'll be handling this as part of https://github.com/dotnet/aspnetcore/issues/8433
Most helpful comment
Just hit this myself, reduces the value proposition of templated components significantly with this limitation at present.