-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
For a given type, derive a new type where all the properties specified in the original type are optional #7355
Comments
I am currently trying to write a specification for the w2ui library and am encountering a similar need. The library eventually uses objects with many fields, and may be initialized by passing an object with any subset of the keys, while default values are plugged for the missing keys. I am not sure how to give this a sensible type without writing two versions of the same, lengthy interface, one where all keys are required and one where all keys are optional. |
I think this is a duplicate of the discussion here: #6613 |
This also seems to be duplicate of #4889. Though I have attempted to suggest a better naming for the concept as described there. |
Duping against #7004 as the closest match of many |
Hey Typescript,
React's current typing for setState (at https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/react/react.d.ts#L128) is somewhat wrong - it asks for type S, while the documentation (at https://facebook.github.io/react/docs/component-api.html#setstate) specifies only that:
That is, callers aren't required to specify all properties on the state, but only a subset of properties that they're updating. Unfortunately, there's currently no way to express this in the type system. People workaround this by defining type S as having all optional properties to begin with, even if some properties should be required.
It'd be great if Typescript had a some type operator that, when applied to an existing type, could construct a new type where all the properties specified in the original type are optional.
Example of use:
Let me know if there's anything else I could clarify, thanks for your time!
The text was updated successfully, but these errors were encountered: