{"record":{"id":"92d60ed16ecb9497","repo":"apple/pkl","slug":"vmtypemismatchexception-union-type-mismatch-for-u","errorCode":null,"errorMessage":"VmTypeMismatchException.Union (type mismatch for union type)","messagePattern":"VmTypeMismatchException\\.Union \\(type mismatch for union type\\)","errorType":"exception","errorClass":"VmTypeMismatchException.Union","httpStatus":null,"severity":"error","filePath":"pkl-core/src/main/java/org/pkl/core/ast/type/TypeNode.java","lineNumber":1126,"sourceCode":"          typeMismatches[i] = e;\n        }\n      }\n\n      // all members failed to type check\n      // if enabled, re-execute type checks to generate power assertions\n      localContext.setInTypeTest(wasInTypeTest);\n      if (VmContext.get(this).getPowerAssertionsEnabled()\n          && (!wasInTypeTest || localContext.hasActiveTracker())) {\n        for (var i = 0; i < elementTypeNodes.length; i++) {\n          try {\n            elementTypeNodes[i].executeEagerly(frame, value);\n          } catch (VmTypeMismatchException e) {\n            typeMismatches[i] = e;\n          }\n        }\n      }\n\n      throw new VmTypeMismatchException.Union(sourceSection, value, this, typeMismatches);\n    }\n  }\n\n  public static final class UnionOfStringLiteralsTypeNode extends ObjectSlotTypeNode {\n    private final Set<String> stringLiterals;\n    private final @Nullable String unionDefault;\n\n    UnionOfStringLiteralsTypeNode(\n        SourceSection sourceSection, int defaultIndex, Set<String> stringLiterals) {\n      super(sourceSection);\n\n      assert !stringLiterals.isEmpty();\n      this.stringLiterals = stringLiterals;\n      if (defaultIndex == -1) {\n        unionDefault = null;\n      } else {\n        unionDefault = stringLiterals.toArray(new String[0])[defaultIndex];\n      }","sourceCodeStart":1108,"sourceCodeEnd":1144,"githubUrl":"https://github.com/apple/pkl/blob/f3efcbfc9b60d30053b0536d664948d7aa1b8673/pkl-core/src/main/java/org/pkl/core/ast/type/TypeNode.java#L1108-L1144","documentation":"Same family as error 80 but thrown from UnionTypeNode.executeEagerly: when a union type's members must be checked eagerly (shallow-forced), Pkl runs each member check and, if all fail, throws VmTypeMismatchException.Union with the collected per-member failures. Eager checking is used when members require it (e.g. two Listing/Mapping members) so lazily-hidden errors cannot escape.","triggerScenarios":"A union type annotation like `Listing<Int>|Listing<String>` is checked eagerly against a value that matches no member; each member's executeEagerly throws and the aggregated VmTypeMismatchException.Union is raised.","commonSituations":"Collections whose elements don't match any listing/mapping alternative in the union; amending or importing modules where a collection property's union annotation no longer matches the produced data.","solutions":["Fix the collection's element types to match one union member.","Adjust the union annotation to cover the actual element types.","Read the per-member sub-exceptions to identify which member came closest and why it rejected the value."],"exampleFix":"// before\nitems: Listing<Int> | Listing<String>  // items contains [1.5]\n// after\nitems: Listing<Int> | Listing<String> | Listing<Float>","handlingStrategy":"validation","validationCode":"// Pkl: verify listing elements eagerly\nfunction checkInts(l: Listing): Listing<Int> =\n  l.getOrElse(0) is Int ? l as Listing<Int> : throw(\"listing elements must be Int\")","typeGuard":"value is Listing<Int> | Listing<String>","tryCatchPattern":null,"preventionTips":["Keep collection element types consistent across union alternatives.","Use a single Listing type when possible instead of unions of collections.","Add element-level checks before assigning to union-typed collections."],"tags":["pkl","type-mismatch","union-type"],"backgroundTag":"type-mismatch","analyzedSha":"f3efcbfc9b60d30053b0536d664948d7aa1b8673","analyzedAt":"2026-09-08T13:10:45.570Z","contentChangedAt":"2026-09-08T13:10:45.570Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}