apple/pkl · error

notASubclassOfTyped

notASubclassOfTyped

Error message

notASubclassOfTyped

What it means

Thrown by Map.toTyped(clazz) when the supplied class is not a subclass of the pkl.base Typed class. toTyped only converts a map into an instance of type-constrained (Typed) classes, so the library rejects any class that isn't a Typed subclass before attempting instantiation.

Source

Thrown at pkl-core/src/main/java/org/pkl/core/stdlib/base/MapNodes.java:255

    @Specialization
    protected VmMap eval(VmMap self) {
      return self;
    }
  }

  public abstract static class toDynamic extends ExternalMethod0Node {
    @Specialization
    protected VmDynamic eval(VmMap self) {
      return self.toDynamic();
    }
  }

  public abstract static class toTyped extends ExternalMethod1Node {
    @Specialization
    @TruffleBoundary
    protected VmTyped eval(VmMap self, VmClass clazz) {
      if (!clazz.isSubclassOf(BaseModule.getTypedClass())) {
        throw exceptionBuilder().evalError("notASubclassOfTyped", clazz).build();
      }

      VmUtils.checkIsInstantiable(clazz, null);

      var result =
          new VmTyped(
              VmUtils.createEmptyMaterializedFrame(),
              clazz.getPrototype(),
              clazz,
              clazz.getMapToTypedMembers());
      result.setExtraStorage(self);
      return result;
    }
  }

  public abstract static class toMapping extends ExternalMethod0Node {
    @Specialization
    protected VmMapping eval(VmMap self) {

View on GitHub (pinned to f3efcbfc9b)

Solutions

  1. Pass a type-constrained class (one declared with a constraint, e.g. `class Person { name: String }`) to toTyped
  2. Use Map.toDynamic instead if you just need a Dynamic object
  3. Check with clazz.isSubclassOf(Typed) or inspect the class declaration for a type constraint
  4. Use toTyped on a class from a .pkl module that actually extends Typed

Example fix

// before
m.toTyped(String) // not a Typed subclass
// after
class Person { name: String }
m.toTyped(Person)
Defensive patterns

Strategy: type-guard

Validate before calling

// Pkl
if (!clazz.isSubclassOf(module(base: Typed))) { /* handle */ }

Type guard

function isTypedClass(clazz: Class): Boolean = clazz.isSubclassOf(Typed)

Prevention

When it happens

Trigger: Calling someMap.toTyped(SomeClass) where SomeClass is a plain open/class module class, an Amended/ Dynamic class, or any class not declared with a type constraint (i.e. not extending Typed).

Common situations: Passing a module class or a base type like String/Int instead of a user-defined type-constrained class, refactoring a class so it no longer has a type constraint, or confusion between toTyped and toDynamic/toMap.

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/b77c19c1dabe312e. Report an issue: GitHub.