apple/pkl · error · VmException
wrongTypeArgumentCount
wrongTypeArgumentCount
Error message
wrongTypeArgumentCount
What it means
This check runs when constructing a declared type reference (e.g. `Foo<Int>` via the reflect/type-argument machinery). It compares the referent's declared type-parameter count with the number of supplied type arguments and throws when they differ.
Source
Thrown at pkl-core/src/main/java/org/pkl/core/stdlib/reflect/DeclaredTypeNodes.java:58
public abstract static class withTypeArguments extends ExternalMethod1Node {
@Specialization
@TruffleBoundary
protected VmTyped eval(VmTyped self, VmList typeArguments) {
var referent = (VmTyped) VmUtils.readMember(self, REFERENT);
checkTypeArgumentCount(referent, typeArguments.getLength(), this);
return MirrorFactories.declaredTypeFactory.create(Pair.of(referent, typeArguments));
}
}
private static void checkTypeArgumentCount(VmTyped referent, int actualCount, PklNode node) {
var extraStorage = referent.getExtraStorage();
var typeParameterCount =
extraStorage instanceof VmClass vmClass
? vmClass.getTypeParameterCount()
: ((VmTypeAlias) extraStorage).getTypeParameterCount();
if (typeParameterCount != actualCount) {
throw new VmExceptionBuilder()
.evalError("wrongTypeArgumentCount", typeParameterCount, actualCount)
.withLocation(node)
.build();
}
}
}
View on GitHub (pinned to f3efcbfc9b)
Solutions
- Supply the exact number of type arguments the referent declares (check `getTypeParameterCount`)
- For Map use two arguments (key, value); for Listing/Set/Dynamic use one; for non-generic classes use zero
- Regenerate client bindings after upgrading the pkl dependency
- Read the counts in the message: expected vs. actual
Example fix
// before new DeclaredType(VmMap, [stringType]) // Map needs 2 // after new DeclaredType(VmMap, [stringType, intType])
Defensive patterns
Strategy: validation
Validate before calling
// Java-side check before constructing the DeclaredType
int expected = referent instanceof VmClass c ? c.getTypeParameterCount() : ((VmTypeAlias) t).getTypeParameterCount();
if (expected != typeArgs.size()) throw new IllegalArgumentException("need " + expected + " type args"); Type guard
boolean hasCorrectArity(VmTyped referent, List<?> args) { return typeParamCount(referent) == args.size(); } Try / catch
try { buildDeclaredType(referent, args); } catch (PklException e) { if (e.getMessage().contains("wrongTypeArgumentCount")) { fixArityAndRetry(); } else throw e; } Prevention
- Check getTypeParameterCount() before passing type arguments
- Regenerate bindings when upgrading the pkl runtime
- Write unit tests constructing each generic type you use
When it happens
Trigger: Building a DeclaredType with the wrong number of type arguments, e.g. `DeclaredType(Listing, [String])` (Listing takes 1) or `DeclaredType(Map, [String])` (Map takes 2).
Common situations: Reflection or tooling code that constructs parameterized types programmatically (PklSwift/Pkl API bindings) after a library upgrade changed type-parameter counts.
Related errors
- JavaType token must be parameterized.
- wrongTypeArgumentCount
- notAParameterizableClass
- wrongTypeArgumentCount
- Did not find expected Java class `%s` on the classpath for P
AI-assisted analysis of apple/pkl@f3efcbfc9b (2026-09-08).
Data as JSON: /api/errors/60c2273a93160486.
Report an issue: GitHub.