Make it easier to express the operations we work with most often:
- When modeling GraphQL types with Flow: Looking up the type of a field in a nested object type, where every field along the way is nullable and optional
- When authoring React components: Looking up the type of a
prop
on another component that we compose, to directly expose it on our component too
- Lookup types:
A[B]
- Function call types:
F(A)
// Objects
type A = {
B?: {
C?: number
}
}
type C1 = A.B?.C // number | undefined
type C2 = A['B']?.['C'] // number | undefined
// Arrays
type D = [number, string[]]
type E = D[1] // string[]
// Functions
type F = number => boolean
type G1 = F(number) // boolean
type G2 = F(3) // boolean
type G3 = F(...[number]) // boolean
// Overloaded Functions
type H = (number, number => boolean)
& (number, string => number)
type I = H(number, number) // boolean
type J = H(number, string) // number
Currently:
// Objects
type A = {
B?: {
C?: number
}
}
type C = $ElementType<
$NonMaybeType<
$ElementType<A, 'B'>
>,
'C'
> // number | undefined
// Arrays
type D = [number, string[]]
type E = $PropertyType<D, 1> // string[]
// Functions
type ReturnType = <A>(() => A) => A
type F = number => boolean
type G1 = $Call<ReturnType, F> // boolean
type G2 = $Call<ReturnType, 3> // boolean