| 1 |
import { unionTypeAnnotation } from "../generated"; |
| 2 |
import removeTypeDuplicates from "../../modifications/flow/removeTypeDuplicates"; |
| 3 |
import type * as t from "../.."; |
| 4 |
|
| 5 |
/** |
| 6 |
* Takes an array of `types` and flattens them, removing duplicates and |
| 7 |
* returns a `UnionTypeAnnotation` node containing them. |
| 8 |
*/ |
| 9 |
export default function createFlowUnionType<T extends t.FlowType>( |
| 10 |
types: [T] | Array<T>, |
| 11 |
): T | t.UnionTypeAnnotation { |
| 12 |
const flattened = removeTypeDuplicates(types); |
| 13 |
|
| 14 |
if (flattened.length === 1) { |
| 15 |
return flattened[0] as T; |
| 16 |
} else { |
| 17 |
return unionTypeAnnotation(flattened); |
| 18 |
} |
| 19 |
} |