{"record":{"id":"6c5e5fa1b8c7185f","repo":"ComposioHQ/composio","slug":"union-types-array-can-not-be-empty","errorCode":null,"errorMessage":"Union types array can not be empty","messagePattern":"Union types array can not be empty","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"warning","filePath":"ts/packages/ts-builders/src/UnionType.ts","lineNumber":83,"sourceCode":"\n      nestedWriter.write('| ');\n      this.variants[i].writeInContext(nestedWriter, TypeContext.UnionMember);\n    }\n  }\n\n  mapVariants<NewVariantType extends TypeBuilder>(\n    callback: (type: VariantType) => NewVariantType\n  ): UnionType<NewVariantType> {\n    return unionType(this.variants.map(v => callback(v)));\n  }\n}\n\nexport function unionType<VariantType extends TypeBuilder = TypeBuilder>(\n  types: VariantType[] | VariantType\n) {\n  if (Array.isArray(types)) {\n    if (types.length === 0) {\n      throw new TypeError('Union types array can not be empty');\n    }\n    const union = new UnionType(types[0]);\n    for (let i = 1; i < types.length; i++) {\n      union.addVariant(types[i]);\n    }\n    return union;\n  }\n  return new UnionType(types);\n}\n","sourceCodeStart":65,"sourceCodeEnd":93,"githubUrl":"https://github.com/ComposioHQ/composio/blob/64b1b85502b1beeb2379e6c9e8bf1104504fa637/ts/packages/ts-builders/src/UnionType.ts#L65-L93","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Guard before calling: if variants.length === 0, use a fallback type (e.g. never or a sensible default TypeBuilder) instead of unionType","Fix the upstream data so at least one variant exists (check why the enum/tool list is empty)","When mapping variants, default the array to a single placeholder variant"],"exampleFix":"// before\nconst t = unionType(variants); // variants may be []\n// after\nconst t = variants.length ? unionType(variants) : literalType('never');","handlingStrategy":"validation","validationCode":"if (!Array.isArray(variants) || variants.length === 0) throw new Error('Provide at least one variant type');","typeGuard":"const hasVariants = (v: unknown): v is TypeBuilder[] => Array.isArray(v) && v.length > 0;","tryCatchPattern":"try { unionType(variants); } catch (e) { if ((e as TypeError).message === 'Union types array can not be empty') { return fallbackType(); } throw e; }","preventionTips":["Guard empty variant lists before code generation","Default empty maps/enums to a placeholder variant","Check filters upstream that can reduce variants to zero"],"tags":["code-generation","type-builders","union","edge-case"],"backgroundTag":"empty-collection-argument","analyzedSha":"64b1b85502b1beeb2379e6c9e8bf1104504fa637","analyzedAt":"2026-08-28T15:39:33.623Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}