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]
- Null chaining for types:
A?.B
// Objects
type A = {
B?: {
C?: number,
D?: string
}
}
type C1 = A.B?.C // number | undefined
type C2 = A['B']?.['C'] // number | undefined
type C3 = A['B']?.['C' | 'D'] // number | string | undefined
// Arrays
type D = [number, string[]]
type E = D[1] // string[]
Currently:
// Objects
type A = {
B?: {
C?: number,
D?: string
}
}
type C1 = $ElementType<
$NonMaybeType<
$ElementType<A, 'B'>
>,
'C'
> // number | undefined
type C3 = C1 | $ElementType<
$NonMaybeType<
$ElementType<A, 'B'>
>,
'D'
> // number | string | undefined
// Arrays
type D = [number, string[]]
type E = $PropertyType<D, 1> // string[]
// Diff types
type A = ?number // number | null | undefined
type B = A - (null | undefined) // number
// Non-maybe types
type A = ?number // number | null | undefined
type B = A! // number
// Function call types
type F = (A, B) => C // (A, B) => C
type C = F(A, B) // C