apple/pkl · error

wrongTypeArgumentCount

wrongTypeArgumentCount

Error message

wrongTypeArgumentCount

What it means

Raised by `UnresolvedTypeNode.Parameterized#checkNumberOfTypeArguments` (called from `execute`): a parameterized class annotation was given a different number of type arguments than the class declares. The `wrongTypeArgumentCount` error reports the expected and actual counts. An interpreter-to-compiler transition precedes the throw, so this is a definite static-usage error in the annotation.

Source

Thrown at pkl-core/src/main/java/org/pkl/core/ast/type/UnresolvedTypeNode.java:343

        for (var i = 0; i < argLength; i++) {
          resolvedTypeArgumentNodes[i] = typeArgumentNodes[i].execute(frame);
        }
        return new TypeAliasTypeNode(sourceSection, typeAlias, resolvedTypeArgumentNodes);
      }

      var module = (VmTyped) baseType;
      throw exceptionBuilder()
          .evalError("notAParameterizableClass", module.getModuleInfo().getModuleName())
          .withSourceSection(typeArgumentNodes[0].sourceSection)
          .build();
    }

    private void checkNumberOfTypeArguments(VmClass clazz) {
      var expectedCount = clazz.getTypeParameterCount();
      var actualCount = typeArgumentNodes.length;
      if (expectedCount != actualCount) {
        CompilerDirectives.transferToInterpreter();
        throw exceptionBuilder()
            .evalError("wrongTypeArgumentCount", expectedCount, actualCount)
            .build();
      }
    }
  }

  public static final class Nullable extends UnresolvedTypeNode {
    @Child private UnresolvedTypeNode elementTypeNode;

    public Nullable(SourceSection sourceSection, UnresolvedTypeNode elementTypeNode) {
      super(sourceSection);
      this.elementTypeNode = elementTypeNode;
    }

    @Override
    public TypeNode execute(VirtualFrame frame) {
      CompilerDirectives.transferToInterpreter();

View on GitHub (pinned to f3efcbfc9b)

Solutions

  1. Supply the exact number of type arguments the error reports as expected (e.g. `Map<String, Int>`).
  2. For functions, include all parameters plus the return type: `Function1<String, Boolean>`.
  3. Update all usages after changing a class's type-parameter list.
  4. Check the class declaration for its `typeParameterCount` to confirm.

Example fix

// before
byName: Map<String> = Map()
// after
byName: Map<String, Person> = Map()
Defensive patterns

Strategy: validation

Validate before calling

function assertTypeArgCount(cls, args) {
  if (args.length !== cls.typeParameterCount) {
    throw new Error(`${cls.name} expects ${cls.typeParameterCount} type arguments, got ${args.length}`);
  }
}

Type guard

function hasCorrectArity(cls, args) { return cls && args && args.length === cls.typeParameterCount; }

Try / catch

try {
  buildParameterizedType(cls, args);
} catch (e) {
  // correct arity and rebuild
}

Prevention

When it happens

Trigger: e.g. `Map<String>` (needs 2), `List<A, B>` (needs 1), or `Function1<A>` (needs 2: param + return) — any mismatch between `clazz.getTypeParameterCount()` and the number of `<...>` arguments written.

Common situations: Forgetting the value type in `Map<K,V>`; omitting the return type of `FunctionN`; adding a variance/default argument out of habit from other languages; refactoring a class to add/remove type parameters without updating usages.

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


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