Typescript like Partial utility type functionality #4609
Replies: 2 comments 5 replies
-
This sounds a lot like the roles proposal: #1711, which would allow you to provide a "role" overlay for a base type. |
Beta Was this translation helpful? Give feedback.
-
I think that there's a lot of different things going on here, and it's worth separating them out. I see at least:
For the first, interfaces are a good solution. If you don't own the type, you can't implement interfaces on it, but roles or extension interfaces might help there. For the second, existing solutions like Automapper do this using reflection. I would like to see some SourceGenerator based solutions, but given the complexity and amount of configuration necessary in this area I wouldn't like to see it in the language itself. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Please excuse me if something like this is already in the works, or is achievable today (if so, I'd love pointers to how). I did google search it, as well as searching issues here, but as
Partial
is already a language construct for partial classes, there was a lot of "noise", and may have missed itIn building many API based applications, I frequently find myself needing to request/return objects that effectively are subsets of the bigger picture (whether to provide more concise API's, to filter sensitive data, etc). Typically I think its pretty common for people to build separate objects and either pull data out into those structures, or map values to/from those types via something like automapper, or manual means.
In more recent development experience, I've had the opportunity to experience typescripts'
Partial<T>
(Partial), and it struck me how you continue to get the type safety, but have the ability to provide subsets of the type around, but still deal with that type in a typesafe way.Seems like
Partial<T>
wouldn't solve every problem (eg, knowing about the existence of a field that exists as a part of the parent object, or restricting which fields could be provided say to a request object), but I think being able to deal with object more generically, while still maintaining type safety could be extremely useful in lots of areas. Also seems like the ability to not have to copy values from somethign like a DTO to api response object would be advantageous.Can record with'ers take records of other types and map fields over with same type and name? (or is it always a anonymous object)
I'm just looking for ways to solve problems more elegantly then instantiating an object, and copying some subset of properties over to new object.
Beta Was this translation helpful? Give feedback.
All reactions