{"record":{"id":"c25f620b4ad87351","repo":"apple/pkl","slug":"type-mismatch","errorCode":null,"errorMessage":"type mismatch","messagePattern":"type mismatch","errorType":"error_code","errorClass":"VmTypeMismatchException.Constraint","httpStatus":null,"severity":"error","filePath":"pkl-core/src/main/java/org/pkl/core/ast/type/TypeNode.java","lineNumber":176,"sourceCode":"  public final boolean isSelfType() {\n    var ret = new MutableBoolean(false);\n    acceptTypeNode(\n        true,\n        typeNode -> {\n          if (typeNode instanceof NonFinalSelfTypeNode || typeNode instanceof FinalSelfTypeNode) {\n            ret.set(true);\n            return false;\n          }\n          return true;\n        });\n    return ret.get();\n  }\n\n  /** Visit child type nodes of this type. */\n  protected abstract boolean acceptTypeNode(boolean visitTypeArguments, TypeNodeConsumer consumer);\n\n  protected VmTypeMismatchException constraintException(Object value, SourceSection sourceSection) {\n    throw new VmTypeMismatchException.Constraint(\n        sourceSection,\n        value,\n        sourceSection,\n        Map.of(new SyntheticNode(sourceSection), List.of(false)));\n  }\n\n  public static TypeNode forClass(SourceSection sourceSection, VmClass clazz) {\n    return clazz.isClosed()\n        ? new FinalClassTypeNode(sourceSection, clazz)\n        : TypeNodeFactory.NonFinalClassTypeNodeGen.create(sourceSection, clazz);\n  }\n\n  public static PType export(@Nullable TypeNode node) {\n    return node != null ? node.doExport() : PType.UNKNOWN;\n  }\n\n  public static VmTyped getMirror(@Nullable TypeNode node) {\n    return node != null ? node.getMirror() : MirrorFactories.unknownTypeFactory.create(null);","sourceCodeStart":158,"sourceCodeEnd":194,"githubUrl":"https://github.com/apple/pkl/blob/f3efcbfc9b60d30053b0536d664948d7aa1b8673/pkl-core/src/main/java/org/pkl/core/ast/type/TypeNode.java#L158-L194","documentation":"TypeNode.constraintException builds the generic \"type mismatch\" VmTypeMismatchException.Constraint used when a value does not conform to a (structural) type node — e.g. a collection element, map value, or type-argument position where the node knows the value was rejected but has no richer per-node data. It records a SyntheticNode over the source section with the failing result.","triggerScenarios":"A value fails to match a parameterized/structural type node (List<Int> receiving a list containing strings, a typed object property receiving the wrong shape, etc.), and the node raises the standard constraint exception.","commonSituations":"Feed-forward configs where an element of a listing/mapping violates its declared element type; API-facing modules validating external JSON/YAML data converted into Pkl values.","solutions":["Locate the source section in the error to find which type position rejected the value.","Validate each element/property of collections against the declared element/property types before assignment.","Fix the offending value or widen the declared type if it is unnecessarily strict.","Use `pkl test` or a REPL to check values against the type incrementally."],"exampleFix":"// before (Pkl)\nports: List<Int> = List(80, \"443\")\n// after\nports: List<Int> = List(80, 443)","handlingStrategy":"type-guard","validationCode":"// Pkl: validate collection elements against the element type\nports.map((p) -> if (p is Int) p else error(\"non-Int port: ${p}\"))","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Validate external data element-by-element before assignment to typed collections.","Prefer precise declared types and let pkl test catch mismatches early.","Convert stringly-typed inputs (e.g. from YAML) to proper types explicitly."],"tags":["type-node","type-mismatch","structural-type","pkl"],"backgroundTag":"type-mismatch","analyzedSha":"f3efcbfc9b60d30053b0536d664948d7aa1b8673","analyzedAt":"2026-09-08T13:10:45.570Z","contentChangedAt":"2026-09-08T13:10:45.570Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}