You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
For a given type, derive a new type where all the properties specified in the original type are optional #7355
The first argument can be an object (containing zero or more keys to update)...
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:
classComponent<S>{setState(state: S?): void;// example syntax - "S?" is the type S but where all properties are optional}interfaceMyState{foo: string;bar: string;}classMyComponentextendsComponent<MyState>{constructor(){this.state={foo: "a",bar: "b"};}functionupdateFoo(){// this should passthis.setState({foo: "newFoo"});// you can also specify that a value is of an optional typeconstupdates: MyState? ={bar: "newBar"};this.setState(updates);// of course, properties not in the original type are still not allowed// this should throw "'nonExistentProperty' does not exist in type 'MyState?'"constbad: MyState? ={nonExistentProperty: 123};}}
Let me know if there's anything else I could clarify, thanks for your time!
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.
Activity
Ptival commentedon Mar 7, 2016
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.
joewood commentedon Mar 14, 2016
I think this is a duplicate of the discussion here: #6613
malibuzios commentedon Mar 24, 2016
This also seems to be duplicate of #4889. Though I have attempted to suggest a better naming for the concept as described there.
RyanCavanaugh commentedon Apr 11, 2016
Duping against #7004 as the closest match of many