apple/pkl · error · GenericParserError

unexpectedTokenForType2

unexpectedTokenForType2

Error message

Unexpected token `{0}`. Expected a type or `{1}`.

What it means

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.

Solutions

  1. Replace the invalid token with a valid type expression (class name, module type, quoted constant type, or function type).
  2. Check for typos or leftover expression syntax in type position.
  3. If a runtime check was intended, ensure it is a type test like `x is String`, not an expression comparison.
  4. Import the module that declares the intended type.

Example fix

// before
if (x is 42) ...

// after
if (x is Int) ...
Defensive patterns

Strategy: validation

Validate before calling

// Token after `is`/`as`/`:` must look like a type (identifier, quoted constant, or ( -> )
function looksLikeType(token) {
  return /^[A-Z`\"]/.test(token) || ['module','nothing','unknown','Int','String','Boolean','Number'].includes(token);
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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

Appendix: source

Thrown at pkl-parser/src/main/java/org/pkl/parser/GenericParserImpl.java:1218

            } else {
              yield new Node(NodeType.PARENTHESIZED_TYPE, children);
            }
          }
          case IDENTIFIER -> {
            var children = new ArrayList<Node>();
            children.add(parseQualifiedIdentifier());
            if (lookahead() == Token.LT) {
              ff(children);
              children.add(parseTypeArgumentList());
            }
            yield new Node(NodeType.DECLARED_TYPE, children);
          }
          case STRING_START ->
              new Node(NodeType.STRING_CONSTANT_TYPE, List.of(parseStringConstant()));
          default -> {
            var text = _lookahead.text(lexer);
            if (expectation != null) {
              throw parserError("unexpectedTokenForType2", text, expectation);
            }
            throw parserError("unexpectedTokenForType", text);
          }
        };

    if (typ.type == NodeType.FUNCTION_TYPE) return typ;
    return parseTypeEnd(typ);
  }

  private Node parseTypeEnd(Node type) {
    var children = new ArrayList<Node>();
    children.add(type);
    // nullable types
    if (lookahead() == Token.QUESTION) {
      ff(children);
      children.add(makeTerminal(next()));
      var res = new Node(NodeType.NULLABLE_TYPE, children);
      return parseTypeEnd(res);

View on GitHub (pinned to f3efcbfc9b)