Proposal: Addition of the equivalent to TypeScripts Partial<T> #5849
Replies: 11 comments 25 replies
-
I'm not sure there could be a good analog in C# or .NET. In TypeScript the types are just facades over the raw dictionaries of JavaScript, the members don't really exist. All |
Beta Was this translation helpful? Give feedback.
-
(@HaloFour there is a blast from the past nick. I miss our long discussions about C# language features, and cross platform UI discussions from back in the day on IRC or maybe it was JabbR. I'd be genuinely curious if you still participate in those sort of communities. Miss those types of discussions) Anyway, that is something I've considered doing as well.. is treating entities as property bags, but even then it seems like a dictionary of key/value pairs often doesn't really do much in the way of enforcing type safety for the types properties that are provided. If I could say this object represented as a sort of dictionary, but this key is of this type, and this key is of that type and know it could be enforced, that would be something I'd be willing to go with (at least for some of my needs). I just don't believe I could really do that easily with dictionary, and that there are existing types that would be more suitable. |
Beta Was this translation helpful? Give feedback.
-
To clarify how such a type would look like in .Net: are you basically asking for What would happen to logic inside property setters, which might include null checking even for non-nullable reference types? What about method parameters, which may or may not be related to properties and fields? Also, depending on your exact requirements, you may be able to write a source generator that does what you want. |
Beta Was this translation helpful? Give feedback.
-
To me this reads like some sort of a proxy/trait so something like this: class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public Color Color { get; set; }
}
class Student as Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
} Then the compiler might generate something like this: class Student
{
public Student(Person person)
{
FirstName = person.FirstName;
LastName = person.LastName;
}
public string FirstName { get; set; }
public string LastName { get; set; }
} But for |
Beta Was this translation helpful? Give feedback.
-
This feels like it is much more reasonable if the base type is a record, as compared to anything which might carry methods that have expectations about the nature of the properties. |
Beta Was this translation helpful? Give feedback.
-
I think the goals for me would be... and it doesn't have to be solved via something identical to
const user = { field1, field2, ... person};
const somePerson = {...user}; |
Beta Was this translation helpful? Give feedback.
-
I actually forgot about this github issue, and was looking to do this very thing... effectively create a source generator that would generate types and their mappers for having subsets of objects that omitted fields etc. This is exactly one of the things I thought of, using a generic attribute. Can you even use generic attributes in generators as generators must target netstandard2.0 (which seems like it supports c# 7.3)? |
Beta Was this translation helpful? Give feedback.
-
So my thoughts were something like this... if we had generic attributes, and could have lambdas as properties for attributes, I could do something like this: (keep in mind just an example of syntax that would be nice... naming would differ) public class User
{
//...
public int Age { get; set; }
//... and [Derivative<User>]
[Except(x=>x.Age)] // even if c# supported lambda as property, not sure how this would work... magic?
public partial class UserWithoutAge
{
} I think that makes for a compelling and clean way of being able to composite types on the fly, and effectively build subsets of types in a clean way that survives refactors etc. I realize right now I could just do I saw someone recommend using |
Beta Was this translation helpful? Give feedback.
-
In the meantime I have created a very basic source generator that creates a new output source with only the properties as optional. If anyone is interested to have a look: https://github.com/Newex/PartialSourceGen |
Beta Was this translation helpful? Give feedback.
-
I need it for merging entities. I have default appSettings with nested properties, and any of them may be overrided. So, I want to write something like So, it's a usefull proposal. |
Beta Was this translation helpful? Give feedback.
-
Code generation is a great idea
C# lacks the ability to hot-swap dll's. It is a real bummer to its ecosystem. Has been since dotnet 2 classic Partials and the dynamic nature of typescript is how we do this there Too many times we use dependency injection instead of a sane plugin architecture. Or get fed up and create a webservice/restful endpoints/rest api/microservice bloat |
Beta Was this translation helpful? Give feedback.
-
When it comes to building API's I've often found the need to return a subset of fields for a given type, but not the entire type. I've also come to really love typescripts
Partial<T>
for things like configuration where we want to allow a user to provide a subset of options from a strongly defined type, but not necessarily the whole thing.Potential Use Cases
Partial<T>
for building a partial updateMany of the cases above, I've had to build libraries of very similar entities/models with certain fields excluded. I feel like
Partial<T>
could largely eliminate the need for lots of unnecessary type hierarchy. Basically any place where you are dealing with a type, and would like to have a subset of fields from a specific type... and be able to count on the fact that the fields supplied will have type safety enforced.(BTW, if there are already suitable ways to achieve the same thing, I'd love to know how... but thus far I've seen nothing like typescripts
Partial<T>
)Beta Was this translation helpful? Give feedback.
All reactions