Secret Flow Types

Roman Krivtsov
Jan 23, 2017 · 4 min read

Of course they are not secret. Recently Flow maintainers exposed some advanced and experimental features, which were previously not documented and used for internal needs. Some of them you probably already came across in articles, issues or source code, but some of them are still not documented.

In this post I’ve tried to collect all utility types, that I found and got with their meanings. Despite that API of these features can (likely) change and you should use them in your projects with caution, some of them are pretty useful in practice.

Accepts object type and returns the new type, which is basically union of it’s keys. For example we can get tuple of keys in object type.

Diff does exactly that you expect it will do. The result type is an object type, which contains only properties, which exist in object type A, but don’t in type B. Good example of usage is default options:

$Shape<T>

Type $Shape<T> has two differences from T: objects of this type cannot contain properties not described in T and may not contain required properties of type T.

You can think of $Shape<T> and T as “overlapping” types.

$Supertype<T>

$Supertype represents supertype of type T. But implementation details of this type are not clear yet, so using of it can lead to mistakes. For example it doesn’t raise errors when it should:

$Subtype<T>

$Subtypes looks better. At least more predictable:

Class<T>

Represents class type, whose instance has type T. For some reasons it doesn’t have sign in the name. So if you called your own type by name it won’t work as expected.

Note also that Class is covariant:

$PropertyType<T, x>

$PropertyType is a useful thing. Let’s assume, that we automatically generated type from some model:

But now we need type somewhere. Instead of copypasting we can use $PropertyType:

$Pred/$Ref

These types are about function predicates. $Pred refines arguments count of function and $Ref refines position of certain type in arguments.

But there is very little information about these types and complete behavior is also not clear, so I wouldn’t recommend to use them.

$Exact<T>

Describes type, that is exactly the same as T. It can be useful, because usually in object types extra properties are allowed:

This behavior is similar to syntax described in docs

$ObjMap<T, F>

$ObjMap allows to map your object type values. It’s not so powerful as new mapped types in TypeScript, but hopefully will be more advanced in future.

Let’s assume that we have type of HTTP request parameters.

And we’d like to define handlers for these params. For example user object should be retrieved from body and referrer is from query string.

Here $ObjMap helps us to map type values to function type to validate new object.

There are also $TupleMap for tuples and $ObjMapi, which at least at this particular example works similarly.

Find me in Twitter

Netscape

A community dedicated to those who use JavaScript every day.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store