Say I have a type:
type State = {
foo: string,
bar: number
};
It would be really useful to be able to specify an update version of the type in which all the fields are optional.
type UpdateState = {
foo?: string,
bar?: number
}
For example in React the argument of setType should be an all optional version of the state type for the component.
Maybe syntax like function setState(state: #State)?
Does this sound feasible and/or a good idea? Or am I missing the obvious way to do this sort of thing already?
What you are describing is implemented, but not documented or technically a "public" API. It's called $Shape and it's already used to type React's setState function. See the definition in lib/react.js.
You can find more about this and other types in this awesome blog post.
Thanks!
@samwgoldman how am I supposed to get knowledge about this $Shape feature?
Most helpful comment
What you are describing is implemented, but not documented or technically a "public" API. It's called
$Shapeand it's already used to type React'ssetStatefunction. See the definition in lib/react.js.You can find more about this and other types in this awesome blog post.