{"record":{"id":"0b4865a254370566","repo":"apple/pkl","slug":"type-mismatch-value-s-class-is-not-a-subclass-of","errorCode":null,"errorMessage":"type mismatch: value's class is not a subclass of expected class","messagePattern":"type mismatch: value's class is not a subclass of expected class","errorType":"exception","errorClass":"VmTypeMismatchException","httpStatus":null,"severity":"error","filePath":"pkl-core/src/main/java/org/pkl/core/ast/type/TypeNode.java","lineNumber":545,"sourceCode":"          PType.MODULE,\n          MirrorFactories.moduleTypeFactory);\n    }\n\n    public static NonFinalSelfTypeNode thisType(SourceSection sourceSection, VmClass clazz) {\n      return new NonFinalSelfTypeNode(\n          sourceSection, clazz, new GetReceiverNode(), PType.THIS, MirrorFactories.thisTypeFactory);\n    }\n\n    @Override\n    protected Object executeLazily(VirtualFrame frame, Object value) {\n      var clazz = ((VmObjectLike) getTargetNode.executeGeneric(frame)).getVmClass();\n\n      if (value instanceof VmTyped typed) {\n        var valueClass = typed.getVmClass();\n        if (clazz.isSuperclassOf(valueClass)) return value;\n      }\n\n      throw typeMismatch(value, clazz);\n    }\n\n    @Override\n    public VmTyped getMirror() {\n      return mirrorFactory.create(null);\n    }\n\n    @Override\n    public boolean doIsEquivalentTo(TypeNode other) {\n      if (!(other instanceof NonFinalSelfTypeNode nonFinalSelfTypeNode)) {\n        return false;\n      }\n      return clazz.equals(nonFinalSelfTypeNode.clazz);\n    }\n\n    @Override\n    protected PType doExport() {\n      return pType;","sourceCodeStart":527,"sourceCodeEnd":563,"githubUrl":"https://github.com/apple/pkl/blob/f3efcbfc9b60d30053b0536d664948d7aa1b8673/pkl-core/src/main/java/org/pkl/core/ast/type/TypeNode.java#L527-L563","documentation":"This is a Pkl type-check failure from the module/this (`module` or open-class self) type check. The VM checks that the assigned value is a VmTyped object whose class is a subclass of the module/this class (NonFinalSelfTypeNode.executeLazily). Because the class is derived at runtime from the target object, this typically fires when a value that is not a module or class instance is used where the module/this type is expected, or a class-conformance rule is violated.","triggerScenarios":"Assigning or passing a value (e.g. a Dynamic, List, String, or Int) into a slot/argument whose declared type is the enclosing module type, an open class `this` type, or a non-final self type; evaluating the type assertion in TypeNode.executeLazily when the value's runtime class is not a subclass of the computed target class.","commonSituations":"Confusing a module with its name (importing and assigning the module object vs. a property), applying `this`-typed constraints to values of other classes, amending/writing properties on an object where the module type constraint is enforced, or a typechecked Pkl library API receiving a non-module receiver.","solutions":["Check the value being assigned at the reported source location; ensure it is an instance of the module/class the type expects (e.g. use `new Foo { ... }` or the correct module reference).","If you meant a plain object, change the declared type to `Dynamic` or the appropriate class instead of the module/this type.","Inspect `is` tests in Pkl: `value is ModuleClass` before assigning, or cast explicitly.","If it comes from a third-party library, update to a version where the API contract matches your usage."],"exampleFix":"// before\nfoo: ModuleType = \"not a module\"\n// after\nfoo: ModuleType = (new ModuleType {})  // or a module instance conforming to ModuleType","handlingStrategy":"type-guard","validationCode":"// Pkl\nif (value is ModuleType) {\n  foo: ModuleType = value\n} else {\n  throw(\"value does not conform to ModuleType\")\n}","typeGuard":"function isModuleType(value: Any): Boolean = value is ModuleType","tryCatchPattern":"// Pkl errors abort evaluation; validate before assignment rather than catching.\n// In Java embedders: catch org.pkl.core.PklException and inspect message for \"type mismatch\".","preventionTips":["Prefer `is` checks at boundaries before assigning to module/this-typed slots","Keep module type constraints on properties that only ever receive module instances","Run `pkl test` fixtures covering each assignment path"],"tags":["pkl","type-mismatch","typecheck","vm"],"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"}