-
Notifications
You must be signed in to change notification settings - Fork 118
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enhancement: [DeepPartial] Support for optionally excluding user-provided types. #361
Comments
Hey @PieterDexper 👋 Thanks for the issue! In your specific example you can simply use If you want to do it on more than 1 layer, I'm happy to create a separate Will it be enough for your use case? |
Hi @Beraliv Thanks for the input! I'm not sure a recursive version of MarkOptional won't result in the same problem. A quick check with:
results in its methods giving the same error. If I understand it correctly with a recursive version of MarkOptional I would be able to use this to exclude specific keys from being made optional, which would be an improvement because I could exclude Timestamp's This isn't ideal because the full setup I have uses DeepPartial within integration tests on data models that describe everything in our system, which results in the issue above with the built in Timestamp type. Since it's built on a document-based database the timestamps can appear within the types at any level of nesting, with arbitrary key names. Those keys may also be in use elsewhere containing a different type. My workaround at the moment re-implements DeepPartial with this added to the various checks in there:
|
Currently ts-essentials doesn't have a way to specify what will be excluded and what not. Currently https://github.com/ts-essentials/ts-essentials/blob/master/lib/deep-partial/index.ts#L5 there are the following types that aren't extended:
I see it now. Can you make up the simple example that explains what you're doing in tests? That will be important to mention so people will understand why it's designed this way and what use cases it covers. Speaking of implementation, I think that can be changed with default generic type. Let's say we will have two generic types now I will think whether I want it to be configurable, maybe I will have generic type differently if anything needs to be added in the nearest future, e.g. Quick PoC in here – https://tsplay.dev/mZ1x1N ( To note, Similarly to I'm also open for the feedback about names of the configuration, it can be There's another related issue where property key is omitted recursively, you might be interested to have a look at it – #104 What do you think? |
Providing a config parameter with a default type to fill it seems like an excellent solution. Maximum flexibility with what gets (or doesn't get) included. A less flexible but slightly simpler version could be something like My use case is a little complex: The system is built on Firestore, which is a document-based noSql database where both frontend and backend talk directly to the database. To support this we have a data model library that contains Typescript types describing the content, as well as the database paths it can be found on. This is combined with a wrapper around the Firestore SDK that uses this information to type the various database interactions.
This ensures the various applications are always in sync on what & where stuff is in the database, provides nice editor support and also avoids the constant casting and specifying generic parameters that the SDK normally requires to support types. Within integration tests we rarely want to completely fill out every single field, most functionality only relies on some of it, so to avoid clutter in the setup, or constant usage of explicit This results in problems with the databases built in data types (like Perhaps for the example something shorter is better, I reckon this situation applies to any system that has their own representations of basic data types: Example: The configuration parameter to DeepPartial makes it possible to exclude these types from being modified recursively:
|
My two cents on the |
※ I think this is part of the same issue, if it is not please let me know and I'll make a new issue. key?: string[] How can I have string instead of (string|undefined) ? |
@goldingdamien correct, it is recursively called for all types including arrays so if you'd like to prevent it, there has to be an option to disable it for arrays so arrays stay as is. I am considering the option that will turn on and off specific types by the names (e.g. date, error, array, etc) I will keep you updated when I have a draft version |
What
I propose to add an additional optional generic parameter to DeepPartial (and possibly other similar types) that allows the user to specify their own types that should be excluded from being made (Deep)Partial on top of the already built in exceptions.
DeepPartial<Type, Exclude>
Any nested type matching
Exclude
would not be made recursively partial.This is helpful in cases where the system has non-standard built in types that can never be partially present, which currently can't be excluded from DeepPartial without fully re-implementing it.
Examples
Additional Info
I'm unsure if this meets the standards of simplicity and general usability, would this be a welcome addition?
The text was updated successfully, but these errors were encountered: