{"record":{"id":"d62e922609cba445","repo":"apple/pkl","slug":"cannotdeclaretypeparameter","errorCode":"cannotDeclareTypeParameter","errorMessage":"cannotDeclareTypeParameter","messagePattern":"cannotDeclareTypeParameter","errorType":"error_code","errorClass":"VmException","httpStatus":null,"severity":"error","filePath":"pkl-core/src/main/java/org/pkl/core/ast/builder/AstBuilder.java","lineNumber":2297,"sourceCode":"      nodes[i] = visitAnnotation(annotations.get(i), annotatedMemberName);\n    }\n    return nodes;\n  }\n\n  public UnresolvedTypeNode visitType(Type type) {\n    return (UnresolvedTypeNode) type.accept(this);\n  }\n\n  public ExpressionNode visitExpr(Expr expr) {\n    return (ExpressionNode) expr.accept(this);\n  }\n\n  @Override\n  public List<TypeParameter> visitTypeParameterList(@Nullable TypeParameterList ctx) {\n    if (ctx == null) return List.of();\n\n    if (!(ctx.parent() instanceof TypeAlias) && !isStdLibModule) {\n      throw exceptionBuilder()\n          .evalError(\"cannotDeclareTypeParameter\")\n          .withSourceSection(createSourceSection(ctx.getParameters().get(0)))\n          .build();\n    }\n\n    var params = ctx.getParameters();\n    var size = params.size();\n    var result = new ArrayList<TypeParameter>(size);\n    for (var i = 0; i < size; i++) {\n      var paramCtx = params.get(i);\n      Variance variance;\n      var nodeVariance = paramCtx.getVariance();\n      if (nodeVariance == null) {\n        variance = TypeParameter.Variance.INVARIANT;\n      } else {\n        variance =\n            switch (nodeVariance) {\n              case IN -> TypeParameter.Variance.CONTRAVARIANT;","sourceCodeStart":2279,"sourceCodeEnd":2315,"githubUrl":"https://github.com/apple/pkl/blob/f3efcbfc9b60d30053b0536d664948d7aa1b8673/pkl-core/src/main/java/org/pkl/core/ast/builder/AstBuilder.java#L2279-L2315","documentation":"Pkl only allows declaring type parameters (`<T>`, `<out T>`) in type aliases (`typealias X<T> = ...`) and in the standard library modules. Anywhere else a type parameter list appears, the AST builder rejects it, since Pkl classes and functions are not generic in user code.","triggerScenarios":"Writing a type parameter list on a class, method, or property outside stdlib, e.g. `class Box<T> { ... }` or `function identity<T>(x: T): T`. The guard checks `!(ctx.parent instanceof TypeAlias) && !isStdLibModule` in visitTypeParameterList.","commonSituations":"Coming from Java/Kotlin/TypeScript generics and assuming Pkl classes are generic; attempting to write a generic collection class in application config code.","solutions":["Remove the type parameter list; use `Any` or a union type instead, e.g. `class Box { value: Any }`.","Put the abstraction in a type alias if a parametric alias fits: `typealias Pair<K, V> = Mapping<K, V>`.","If you truly need generics in stdlib-style code, contribute/use the stdlib path (not available to user modules)."],"exampleFix":"// before\nclass Box<T> { value: T }\n// after\nclass Box { value: Any }\n// or\ntypealias BoxOf<T> = Mapping<String, T>","handlingStrategy":"validation","validationCode":"// reject generic class/function syntax before submitting to Pkl\nif (/\\b(class|function)\\s+\\w+\\s*</.test(src))\n  throw new Error('Pkl only supports type parameters in typealias declarations');","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Remember: Pkl user code is not generic — only typealiases and stdlib take type parameters.","Model genericity with `Any`, unions, or typealias-level parameters.","When migrating from Java/TS, strip `<T>` clauses during translation."],"tags":["pkl","generics","type-parameters","compile-error"],"backgroundTag":"type-parameter-not-allowed","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"}