apple/pkl · error · VmTypeMismatchException
type mismatch: value is not of type Typed
Error message
type mismatch: value is not of type Typed
What it means
Pkl `Typed` type check failure. The `Typed` type is the base type of all class instances created with `new`; the check (`value instanceof VmTyped`) rejects anything else. The value was expected to be a Typed (class-instance) object but was a Dynamic, Listing, Mapping, primitive, function, etc.
Source
Thrown at pkl-core/src/main/java/org/pkl/core/ast/type/TypeNode.java:649
return new PType.StringLiteral(literal);
}
@Override
protected boolean acceptTypeNode(boolean visitTypeArguments, TypeNodeConsumer consumer) {
return consumer.accept(this);
}
}
public static final class TypedTypeNode extends ObjectSlotTypeNode {
public TypedTypeNode(SourceSection sourceSection) {
super(sourceSection);
}
@Override
protected Object executeLazily(VirtualFrame frame, Object value) {
if (value instanceof VmTyped) return value;
throw typeMismatch(value, BaseModule.getTypedClass());
}
@Override
public VmClass getVmClass() {
return BaseModule.getTypedClass();
}
@Override
public boolean doIsEquivalentTo(TypeNode other) {
return other instanceof TypedTypeNode;
}
@Override
protected boolean acceptTypeNode(boolean visitTypeArguments, TypeNodeConsumer consumer) {
return consumer.accept(this);
}
}
View on GitHub (pinned to f3efcbfc9b)
Solutions
- Replace the value with a class instance created via `new SomeClass { ... }`.
- If any object is acceptable, change the declared type from `Typed` to `Dynamic` or `Object`.
- If the value is currently a Dynamic, convert it: define a class and construct with `new`.
- Check for accidental assignment of a Listing/Mapping, which is not `Typed`.
Example fix
// before
obj: Typed = new Dynamic { name = "x" }
// after
obj: Typed = new Person { name = "x" } Defensive patterns
Strategy: type-guard
Validate before calling
// Pkl
if (value is Typed) {
obj: Typed = value
} Type guard
function isTypedObject(value: Any): Boolean = value is Typed
Try / catch
// Guard with `is Typed` before assignment; embedders: catch PklException filtered on 'not of type Typed'.
Prevention
- Remember Dynamic/List/Mapping/primitives are not `Typed`
- Construct class instances with `new`, not object literals
- Declare the narrowest accurate type instead of `Typed` when possible
When it happens
Trigger: Declaring a property as `Typed` and assigning a Dynamic object (`new Dynamic {}`), a primitive, a collection, or null; passing a non-class-instance into an API expecting `Typed`.
Common situations: Migrating code from `Dynamic` to typed classes and forgetting to convert `new Dynamic {...}` to `new SomeClass {...}`; returning an object literal where a class instance is required; null default where Typed has no default.
Understand the failure class
Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.
Related errors
- type mismatch: value does not conform to declared type (clas
- type mismatch: value's class is not a subclass of expected c
- type mismatch: value is not the expected string literal
- type mismatch: value is not of type Dynamic
- type mismatch: value's class differs from expected class
AI-assisted analysis of apple/pkl@f3efcbfc9b (2026-09-08).
Data as JSON: /api/errors/bdae42307702223e.
Report an issue: GitHub.