Search code examples
javascriptflowtype

How to use an inner Flow type?


I have a generated file which exports MyComplicatedType, which has a parameter child with the type I actually want to use. The inner type is not named or exported in the generated file.

export type MyComplicatedType = {|
  +child: $ReadOnlyArray<{|
   // Complicated type
  |}>
|};

Is there any way I can use the type of child without copy/pasting? Since the code is generated, I would have to re-copy/paste every time I rebuild otherwise.


Solution

  • You can use a $Call to extract it.

    Example:

    type TypeNeeded = $Call<
         <T>({
                 +child:$ReadOnlyArray<T>
               })=>T,
         MyComplicatedType
    >
    

    To retrieve what you have in the $ReadOnlyArray.

    You can see more complicated examples in the document: https://flow.org/en/docs/types/utilities/#toc-call