ComposioHQ/composio · warning · TypeError

Union types array can not be empty

Error message

Union types array can not be empty

What it means

unionType in ts-builders builds a TS union literal from variant TypeBuilders; an empty array has no first variant to seed the UnionType, so it throws TypeError('Union types array can not be empty') rather than producing an invalid union type.

Source

Thrown at ts/packages/ts-builders/src/UnionType.ts:83

      nestedWriter.write('| ');
      this.variants[i].writeInContext(nestedWriter, TypeContext.UnionMember);
    }
  }

  mapVariants<NewVariantType extends TypeBuilder>(
    callback: (type: VariantType) => NewVariantType
  ): UnionType<NewVariantType> {
    return unionType(this.variants.map(v => callback(v)));
  }
}

export function unionType<VariantType extends TypeBuilder = TypeBuilder>(
  types: VariantType[] | VariantType
) {
  if (Array.isArray(types)) {
    if (types.length === 0) {
      throw new TypeError('Union types array can not be empty');
    }
    const union = new UnionType(types[0]);
    for (let i = 1; i < types.length; i++) {
      union.addVariant(types[i]);
    }
    return union;
  }
  return new UnionType(types);
}

View on GitHub (pinned to 64b1b85502)

Solutions

  1. Guard before calling: if variants.length === 0, use a fallback type (e.g. never or a sensible default TypeBuilder) instead of unionType
  2. Fix the upstream data so at least one variant exists (check why the enum/tool list is empty)
  3. When mapping variants, default the array to a single placeholder variant

Example fix

// before
const t = unionType(variants); // variants may be []
// after
const t = variants.length ? unionType(variants) : literalType('never');
Defensive patterns

Strategy: validation

Validate before calling

if (!Array.isArray(variants) || variants.length === 0) throw new Error('Provide at least one variant type');

Type guard

const hasVariants = (v: unknown): v is TypeBuilder[] => Array.isArray(v) && v.length > 0;

Try / catch

try { unionType(variants); } catch (e) { if ((e as TypeError).message === 'Union types array can not be empty') { return fallbackType(); } throw e; }

Prevention

When it happens

Trigger: Calling unionType([]), or higher-level generators (generateToolkitUnionType, union, unionParam, unionProp, mapVariants) when the variants list filters down to zero — e.g. a toolkit with no tools, an enum/mapping with no cases, or all variants filtered out.

Common situations: Code-generating from empty metadata: empty enum, empty variant map, filtering a variants array before passing it; generating a toolkit union when the toolkit exposes no tools.


AI-assisted analysis of ComposioHQ/composio@64b1b85502 (2026-08-28). Data as JSON: /api/errors/6c5e5fa1b8c7185f. Report an issue: GitHub.