Flow: Given "type T = ?{鈥" how can I get "type U = {鈥", i.e., "type U = T \setminus null \setminus undefined"?

Created on 28 Jun 2017  路  4Comments  路  Source: facebook/flow

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.

Most helpful comment

Should I add that to https://flow.org/en/docs/types/utilities/ or is it documented somewhere else?

All 4 comments

$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?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

l2silver picture l2silver  路  3Comments

ctrlplusb picture ctrlplusb  路  3Comments

glenjamin picture glenjamin  路  3Comments

philikon picture philikon  路  3Comments

jamiebuilds picture jamiebuilds  路  3Comments