Relay generates nice Flow types for me like
type T = {| user: ?{ firstName: string } |};
.
I'd like to be able to say a component accepts { firstName: string } without hard-coding the firstName: string in. I can use type U = $PropertyType<T, "user"> to dynamically get type U = ?{ firstName: string }, but would like another operator to let me require that the object is not null or undefined? Is there anything like this? It looks like $Diff<A, B> almost does this, but only works when A and B are both Objects.
$NonMaybeType<$PropertyType<T,"user">> should give you what you want.
Perfect!
i used this before i've heard about $NonMaybeType :smiley_cat: :
type _Definitely<T, U: ?T> = T;
type Definitely<T> = _Definitely<*, T>;
type T = {| user: ?{ firstName: string } |};
type U = Definitely<$PropertyType<T, 'user'>>;
({ firstName: 'foo' }: U);
Should I add that to https://flow.org/en/docs/types/utilities/ or is it documented somewhere else?
Most helpful comment
Should I add that to https://flow.org/en/docs/types/utilities/ or is it documented somewhere else?