{"record":{"id":"a1294ee580731000","repo":"apple/pkl","slug":"unexpectedtokenfortype2","errorCode":"unexpectedTokenForType2","errorMessage":"Unexpected token `{0}`. Expected a type or `{1}`.","messagePattern":"Unexpected token `(.+?)`\\. Expected a type or `(.+?)`\\.","errorType":"exception","errorClass":"GenericParserError","httpStatus":null,"severity":"error","filePath":"pkl-parser/src/main/java/org/pkl/parser/GenericParserImpl.java","lineNumber":1218,"sourceCode":"            } else {\n              yield new Node(NodeType.PARENTHESIZED_TYPE, children);\n            }\n          }\n          case IDENTIFIER -> {\n            var children = new ArrayList<Node>();\n            children.add(parseQualifiedIdentifier());\n            if (lookahead() == Token.LT) {\n              ff(children);\n              children.add(parseTypeArgumentList());\n            }\n            yield new Node(NodeType.DECLARED_TYPE, children);\n          }\n          case STRING_START ->\n              new Node(NodeType.STRING_CONSTANT_TYPE, List.of(parseStringConstant()));\n          default -> {\n            var text = _lookahead.text(lexer);\n            if (expectation != null) {\n              throw parserError(\"unexpectedTokenForType2\", text, expectation);\n            }\n            throw parserError(\"unexpectedTokenForType\", text);\n          }\n        };\n\n    if (typ.type == NodeType.FUNCTION_TYPE) return typ;\n    return parseTypeEnd(typ);\n  }\n\n  private Node parseTypeEnd(Node type) {\n    var children = new ArrayList<Node>();\n    children.add(type);\n    // nullable types\n    if (lookahead() == Token.QUESTION) {\n      ff(children);\n      children.add(makeTerminal(next()));\n      var res = new Node(NodeType.NULLABLE_TYPE, children);\n      return parseTypeEnd(res);","sourceCodeStart":1200,"sourceCodeEnd":1236,"githubUrl":"https://github.com/apple/pkl/blob/f3efcbfc9b60d30053b0536d664948d7aa1b8673/pkl-parser/src/main/java/org/pkl/parser/GenericParserImpl.java#L1200-L1236","documentation":"When parseType encounters a token that cannot begin a type and an expectation string was supplied (used in contexts like `is`, `as`, or parameter types to hint what was expected), it throws unexpectedTokenForType2 with the offending token text plus the expectation. It is the expectation-aware variant of unexpectedTokenForType.","triggerScenarios":"A type position contains an invalid token — e.g. `x: (1 + 2)`, `is 42`, `as =foo` — where a type name, module literal, string constant type, or function type was required.","commonSituations":"Using an expression where a type is required (e.g. in `is`/`as` or type annotations); typos in type names; accidentally typing `=` or a number where a class/module type should be; removed type declarations leaving dangling references.","solutions":["Replace the invalid token with a valid type expression (class name, module type, quoted constant type, or function type).","Check for typos or leftover expression syntax in type position.","If a runtime check was intended, ensure it is a type test like `x is String`, not an expression comparison.","Import the module that declares the intended type."],"exampleFix":"// before\nif (x is 42) ...\n\n// after\nif (x is Int) ...","handlingStrategy":"validation","validationCode":"// Token after `is`/`as`/`:` must look like a type (identifier, quoted constant, or ( -> )\nfunction looksLikeType(token) {\n  return /^[A-Z`\\\"]/.test(token) || ['module','nothing','unknown','Int','String','Boolean','Number'].includes(token);\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never put runtime expressions where a type is expected (e.g. `x is (a == b)`).","Use `is`/`as` only with declared or builtin types.","Enable Pkl language-server diagnostics in your editor to catch these at write time."],"tags":["pkl","parser","syntax-error","type-annotation"],"backgroundTag":"invalid-argument-value","analyzedSha":"f3efcbfc9b60d30053b0536d664948d7aa1b8673","analyzedAt":"2026-09-08T13:10:45.570Z","contentChangedAt":"2026-09-08T13:10:45.570Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}