{"record":{"id":"032eb5766d790ec7","repo":"apple/pkl","slug":"type-mismatch-value-is-not-of-type-string","errorCode":null,"errorMessage":"type mismatch: value is not of type String","messagePattern":"type mismatch: value is not of type String","errorType":"exception","errorClass":"VmTypeMismatchException","httpStatus":null,"severity":"error","filePath":"pkl-core/src/main/java/org/pkl/core/ast/type/TypeNode.java","lineNumber":3042,"sourceCode":"      return other instanceof AnyTypeNode;\n    }\n\n    @Override\n    protected boolean acceptTypeNode(boolean visitTypeArguments, TypeNodeConsumer consumer) {\n      return consumer.accept(this);\n    }\n  }\n\n  public static final class StringTypeNode extends ObjectSlotTypeNode {\n    public StringTypeNode(SourceSection sourceSection) {\n      super(sourceSection);\n    }\n\n    @Override\n    protected Object executeLazily(VirtualFrame frame, Object value) {\n      if (value instanceof String) return value;\n\n      throw typeMismatch(value, BaseModule.getStringClass());\n    }\n\n    @Override\n    public VmClass getVmClass() {\n      return BaseModule.getStringClass();\n    }\n\n    @Override\n    public boolean doIsEquivalentTo(TypeNode other) {\n      return other instanceof StringTypeNode;\n    }\n\n    @Override\n    protected boolean acceptTypeNode(boolean visitTypeArguments, TypeNodeConsumer consumer) {\n      return consumer.accept(this);\n    }\n  }\n","sourceCodeStart":3024,"sourceCodeEnd":3060,"githubUrl":"https://github.com/apple/pkl/blob/f3efcbfc9b60d30053b0536d664948d7aa1b8673/pkl-core/src/main/java/org/pkl/core/ast/type/TypeNode.java#L3024-L3060","documentation":"The String typecheck node's executeLazily accepts a value only if it is a Java String (Pkl String); anything else fails with 'type mismatch: value is not of type String' via typeMismatch(value, BaseModule.getStringClass()). Pkl does not coerce numbers/booleans to strings implicitly.","triggerScenarios":"Assigning a number, Boolean, null, or object to a property typed String, e.g. `name: String = 42` or an imported JSON field that is a number.","commonSituations":"JSON/YAML imports where a field is sometimes numeric (`port: 8080`); interpolation forgotten (using `x` instead of `\"\\(x)\"`); env-var substitution yielding null.","solutions":["Convert explicitly with `.toString()` or string interpolation `\"\\(value)\"`","Fix the source value to be a string (quote it in the imported data)","Relax the declaration type if a non-string is legitimate (e.g. `String|Int`)","Validate with `v is String` before assignment"],"exampleFix":"// before\nname: String = 42\n// after\nname: String = \"42\"\n// or\nname: String|Int = 42","handlingStrategy":"validation","validationCode":"// pkl: assert(value is String)\nfunction isString(v) { return typeof v === 'string'; }","typeGuard":"function isString(v) { return typeof v === 'string'; }","tryCatchPattern":"try { checkString(value); } catch (e) { log(`expected String, got ${typeof value}: ${value}`); throw e; }","preventionTips":["Quote values in imported YAML/JSON when they must be strings","Use string interpolation \"\\(x)\" instead of raw non-string values","Coerce env/CLI inputs with .toString() before assignment"],"tags":["pkl","string","type-mismatch"],"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"}