{"record":{"id":"dfed35bcc33a7455","repo":"apple/pkl","slug":"typemismatch-dfed35","errorCode":"typeMismatch","errorMessage":"Expected value of type `{0}`, but got type `{1}`.","messagePattern":"Expected value of type `(.+?)`, but got type `(.+?)`\\.","errorType":"error_code","errorClass":"VmException","httpStatus":null,"severity":"error","filePath":"pkl-core/src/main/java/org/pkl/core/ast/expression/ternary/IfElseNode.java","lineNumber":57,"sourceCode":"    super(sourceSection);\n    this.conditionNode = conditionNode;\n    this.thenNode = thenNode;\n    this.elseNode = elseNode;\n  }\n\n  @Override\n  public Object executeGeneric(VirtualFrame frame) {\n    return evaluateCondition(frame)\n        ? thenNode.executeGeneric(frame)\n        : elseNode.executeGeneric(frame);\n  }\n\n  private boolean evaluateCondition(VirtualFrame frame) {\n    try {\n      return conditionNode.executeBoolean(frame);\n    } catch (UnexpectedResultException e) {\n      CompilerDirectives.transferToInterpreter();\n      throw exceptionBuilder()\n          .typeMismatch(e.getResult(), BaseModule.getBooleanClass())\n          .withSourceSection(conditionNode.getSourceSection())\n          .build();\n    }\n  }\n}\n","sourceCodeStart":39,"sourceCodeEnd":64,"githubUrl":"https://github.com/apple/pkl/blob/f3efcbfc9b60d30053b0536d664948d7aa1b8673/pkl-core/src/main/java/org/pkl/core/ast/expression/ternary/IfElseNode.java#L39-L64","documentation":"The condition of an `if (cond) ... else ...` expression must be a Boolean. IfElseNode.evaluateCondition calls conditionNode.executeBoolean; when the condition expression produces a non-Boolean value (UnexpectedResultException), it throws a typeMismatch error stating the expected type `Boolean` and the actual type of the result.","triggerScenarios":"`if (cond)` where cond evaluates to a non-Boolean: e.g. `if (x)` with `x: Int`, `if (list.isEmpty)` typo'd to `if (list)`, or `if (someNullable)` where the value is String/Int/Null instead of a Boolean predicate call.","commonSituations":"Migrating from a dynamic-language habit of truthy checks (`if (name)`) into Pkl which requires an explicit Boolean; calling a property instead of a method (forgetting `()` on `isEmpty()`); comparing with `=`/`==` mistakes that yield non-Boolean types.","solutions":["Make the condition explicitly Boolean, e.g. `if (x != null)`, `if (!list.isEmpty())`, `if (value == expected)`","Fix forgotten parentheses: use `list.isEmpty()` (a Boolean method) rather than `list.isEmpty`","Check the condition's declared type in its definition and convert it (e.g. `x > 0` instead of `x`)","If the condition may be null, write `if (cond != null && cond)` style guards"],"exampleFix":"// before\nname: String = \"srv\"\nif (name) { ... } // ERROR: typeMismatch, got String\n// after\nif (!name.isEmpty()) { ... }","handlingStrategy":"validation","validationCode":"// ensure the if-condition is a Boolean expression in Pkl\n// bad:  if (name)\n// good: if (!name.isEmpty())\n// good: if (x != null)","typeGuard":"null","tryCatchPattern":"null","preventionTips":["Always write explicit comparisons; Pkl has no truthy/falsy coercion","Beware property vs method: `isEmpty` vs `isEmpty()`","Type-check conditions by annotating helper predicates with `: Boolean`"],"tags":["pkl","type-mismatch","boolean","conditional"],"backgroundTag":"type-mismatch","analyzedSha":"f3efcbfc9b60d30053b0536d664948d7aa1b8673","analyzedAt":"2026-09-08T13:10:45.570Z","contentChangedAt":"2026-09-08T13:10:45.570Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}