createTSUnionType.js 677 Bytes
Newer Older
Rosanny Sihombing's avatar
Rosanny Sihombing committed
1
2
3
4
5
6
7
8
9
10
11
"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = createTSUnionType;

var _generated = require("../generated");

var _removeTypeDuplicates = require("../../modifications/typescript/removeTypeDuplicates");

12
13
var _index = require("../../validators/generated/index");

Rosanny Sihombing's avatar
Rosanny Sihombing committed
14
function createTSUnionType(typeAnnotations) {
15
16
17
  const types = typeAnnotations.map(type => {
    return (0, _index.isTSTypeAnnotation)(type) ? type.typeAnnotation : type;
  });
Rosanny Sihombing's avatar
Rosanny Sihombing committed
18
19
20
21
22
23
24
25
  const flattened = (0, _removeTypeDuplicates.default)(types);

  if (flattened.length === 1) {
    return flattened[0];
  } else {
    return (0, _generated.tsUnionType)(flattened);
  }
}