{"record":{"id":"09fd00ac48614f25","repo":"apple/pkl","slug":"expected-d-type-arguments-for-s-but-got-d","errorCode":null,"errorMessage":"Expected %d type arguments for `%s`, but got %d.","messagePattern":"Expected (.+?) type arguments for `(.+?)`, but got (.+?)\\.","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"pkl-config-java/src/main/java/org/pkl/config/java/mapper/Types.java","lineNumber":41,"sourceCode":"import org.pkl.core.Pair;\n\n/**\n * A factory for parameterized type literals such as {@code List<String>} or {@code MyClass<Foo,\n * Bar>}. Used to express the desired target type in {@link ValueMapper#map(Object, Type)}.\n */\npublic final class Types {\n  private Types() {}\n\n  public static ParameterizedType parameterizedType(Class<?> rawType, Type... typeArguments) {\n    var typeParamsCount = rawType.getTypeParameters().length;\n    if (typeParamsCount == 0) {\n      throw new IllegalArgumentException(\n          String.format(\n              \"Cannot parameterize `%s` because it does not have any type parameters.\",\n              rawType.getTypeName()));\n    }\n    if (typeArguments.length != typeParamsCount) {\n      throw new IllegalArgumentException(\n          String.format(\n              \"Expected %d type arguments for `%s`, but got %d.\",\n              typeParamsCount, rawType.getTypeName(), typeArguments.length));\n    }\n    for (Type arg : typeArguments) {\n      if (arg instanceof Class<?> clazz) {\n        if (clazz.isPrimitive()) {\n          throw new IllegalArgumentException(\n              String.format(\n                  \"`%s.class` is not a valid type argument. Did you mean `%s.class`?\",\n                  clazz, Reflection.toWrapperType(clazz).getSimpleName()));\n        }\n      }\n    }\n    try {\n      return (ParameterizedType) TypeFactory.parameterizedClass(rawType, typeArguments);\n    } catch (TypeArgumentNotInBoundException e) {\n      throw new IllegalArgumentException(","sourceCodeStart":23,"sourceCodeEnd":59,"githubUrl":"https://github.com/apple/pkl/blob/f3efcbfc9b60d30053b0536d664948d7aa1b8673/pkl-config-java/src/main/java/org/pkl/config/java/mapper/Types.java#L23-L59","documentation":"parameterizedType() requires one type argument per declared type parameter of the raw class. Supplying fewer or more arguments than getTypeParameters().length throws with the expected and actual counts.","triggerScenarios":"parameterizedType(Map.class, String.class) (1 arg for 2 params) or parameterizedType(List.class, String.class, Integer.class) (2 args for 1 param).","commonSituations":"Forgetting Map has two parameters; adding an extra argument after a copy-paste; confusion between Optional (1 param) and Pair-like types (2 params).","solutions":["Supply exactly as many type arguments as the raw class declares.","For Map, pass both key and value argument types.","Verify the count against rawType.getTypeParameters().length before calling."],"exampleFix":"// before\nTypes.parameterizedType(Map.class, String.class);\n// after\nTypes.parameterizedType(Map.class, String.class, Integer.class);","handlingStrategy":"validation","validationCode":"if (args.length != rawType.getTypeParameters().length) throw new IllegalArgumentException(\"wrong number of type arguments\");","typeGuard":"static int expectedArgCount(Class<?> c) { return c.getTypeParameters().length; }","tryCatchPattern":"try { Types.parameterizedType(raw, args); } catch (IllegalArgumentException e) { /* correct arity */ }","preventionTips":["Remember Map/Pair-like types need two arguments","Build target types with Types helpers (listOf, setOf, optionalOf, pairOf) instead of by hand"],"tags":["java","generics","arity-mismatch","illegal-argument"],"backgroundTag":"missing-required-argument","analyzedSha":"f3efcbfc9b60d30053b0536d664948d7aa1b8673","analyzedAt":"2026-09-08T13:10:45.570Z","contentChangedAt":"2026-09-08T13:10:45.570Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}