apple/pkl · error · ParserError

typeAnnotationInAmends

typeAnnotationInAmends

Error message

Properties with type annotations cannot have object bodies.

To define both a type annotation and an object body, try using assignment instead.
For example: `obj: Bird = new { ... }`.

What it means

A class property that has a type annotation cannot also directly amend (have `{ ... }` object bodies) without an assignment. Pkl requires `prop: Type = new { ... }` or an amends-style assignment; a bare `prop: Type { ... }` is rejected with a hint to use assignment instead.

Solutions

  1. Add an explicit assignment: use `obj: Bird = new { ... }` when constructing a new value, per the error hint.
  2. If the intent is to AMEND an inherited value, write `obj: Bird = super { ... }` (or drop the annotation if the type is already known).
  3. If no type annotation is needed, remove `: Type` and keep the plain `obj { ... }` amend form.
  4. Only when the property has no `=` assignment may `{ ... }` bodies appear — so restructure to satisfy one of those two shapes.

Example fix

// before
class Keeper {
  bird: Bird { name = "tweety" }
}
// after
class Keeper {
  bird: Bird = new { name = "tweety" }
}
Defensive patterns

Strategy: validation

Validate before calling

// Detect 'prop: Type {' shape before parsing and suggest the fix
const TYPED_AMEND = /^\s*[A-Za-z_]\w*\s*:\s*[A-Z][\w.<>]\w*(\s*\{)/;
// if (TYPED_AMEND.test(line)) suggest(line.replace(/\{/, '= new {'));

Prevention

When it happens

Trigger: Writing `bird: Bird { ... }` inside a class body intending to define a typed default object; converting `x { ... }` property-amend syntax to typed form but omitting `=`; mixing type-annotation style with the amending `{ }` shorthand.

Common situations: Developers coming from untyped Pkl properties (`obj { ... }`) adding a type annotation without switching to assignment; scaffolding typed config defaults; IDE quick-fixes that insert the type but keep the brace body.

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/a5ef9f04713487e9. Report an issue: GitHub.

Appendix: source

Thrown at pkl-parser/src/main/java/org/pkl/parser/ParserImpl.java:462

    var start = header.span(name.span());
    var children = new ArrayList<@Nullable Node>();
    children.add(header.docComment);
    children.addAll(header.annotations);
    var modifiersOffset = header.annotations.size() + 1;
    children.addAll(header.modifiers);
    var nameOffset = modifiersOffset + header.modifiers.size();
    TypeAnnotation typeAnnotation = null;
    Expr expr = null;
    var bodies = new ArrayList<ObjectBody>();
    if (lookahead == Token.COLON) {
      typeAnnotation = parseTypeAnnotation();
    }
    if (lookahead == Token.ASSIGN) {
      next();
      expr = parseExpr();
    } else if (lookahead == Token.LBRACE) {
      if (typeAnnotation != null) {
        throw parserError("typeAnnotationInAmends");
      }
      while (lookahead == Token.LBRACE) {
        bodies.add(parseObjectBody());
      }
    }
    children.add(name);
    children.add(typeAnnotation);
    children.add(expr);
    children.addAll(bodies);
    if (expr != null) {
      return new ClassProperty(children, modifiersOffset, nameOffset, start.endWith(expr.span()));
    }
    if (!bodies.isEmpty()) {
      return new ClassProperty(
          children,
          modifiersOffset,
          nameOffset,
          start.endWith(bodies.get(bodies.size() - 1).span()));

View on GitHub (pinned to f3efcbfc9b)