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

  1. Replace the value with a class instance created via `new SomeClass { ... }`.
  2. If any object is acceptable, change the declared type from `Typed` to `Dynamic` or `Object`.
  3. If the value is currently a Dynamic, convert it: define a class and construct with `new`.
  4. 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

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


AI-assisted analysis of apple/pkl@f3efcbfc9b (2026-09-08). Data as JSON: /api/errors/bdae42307702223e. Report an issue: GitHub.