{"record":{"id":"601328a1b7257c6c","repo":"apple/pkl","slug":"typeannotationinamends","errorCode":"typeAnnotationInAmends","errorMessage":"Properties with type annotations cannot have object bodies.","messagePattern":"Properties with type annotations cannot have object bodies\\.","errorType":"exception","errorClass":"GenericParserError","httpStatus":null,"severity":"error","filePath":"pkl-parser/src/main/java/org/pkl/parser/GenericParserImpl.java","lineNumber":326,"sourceCode":"    headerBegin.add(parseIdentifier());\n    header.add(new Node(NodeType.CLASS_PROPERTY_HEADER_BEGIN, headerBegin));\n    var hasTypeAnnotation = false;\n    if (lookahead() == Token.COLON) {\n      ff(header);\n      header.add(parseTypeAnnotation());\n      hasTypeAnnotation = true;\n    }\n    children.add(new Node(NodeType.CLASS_PROPERTY_HEADER, header));\n    if (lookahead() == Token.ASSIGN) {\n      ff(children);\n      children.add(makeTerminal(next()));\n      var body = new ArrayList<Node>();\n      ff(body);\n      body.add(parseExpr());\n      children.add(new Node(NodeType.CLASS_PROPERTY_BODY, body));\n    } else if (lookahead() == Token.LBRACE) {\n      if (hasTypeAnnotation) {\n        throw parserError(\"typeAnnotationInAmends\");\n      }\n      while (lookahead() == Token.LBRACE) {\n        ff(children);\n        children.add(parseObjectBody());\n      }\n    }\n    return new Node(NodeType.CLASS_PROPERTY, children);\n  }\n\n  private Node parseClassMethod(List<Node> preChildren) {\n    var headerParts = getHeaderParts(preChildren);\n    var children = new ArrayList<>(headerParts.preffixes);\n    var headers = new ArrayList<Node>();\n    if (headerParts.modifierList != null) {\n      headers.add(headerParts.modifierList);\n    }\n    expect(Token.FUNCTION, headers, \"unexpectedToken\", \"function\");\n    ff(headers);","sourceCodeStart":308,"sourceCodeEnd":344,"githubUrl":"https://github.com/apple/pkl/blob/f3efcbfc9b60d30053b0536d664948d7aa1b8673/pkl-parser/src/main/java/org/pkl/parser/GenericParserImpl.java#L308-L344","documentation":"A class property declared with a type annotation (`name: Type`) was followed by an object (amends) body `{ ... }`. The Pkl grammar forbids combining a type annotation with an amends body in a class property, so the parser rejects it.","triggerScenarios":"parseClassProperty sees `lookahead() == Token.LBRACE` after a property that `hasTypeAnnotation`, e.g. `foo: String { ... }` inside a class or module.","commonSituations":"Mixing Java/TypeScript-style typed syntax with Pkl amends syntax; intending a default value and accidentally writing an amends block; copying an object-literal pattern into a class property.","solutions":["Remove the type annotation if the property should amend another object (`foo { ... }`)","Remove the amends body and give a typed default value instead (`foo: String = \"h\"`)","Declare the base type in the amends target, not on the property"],"exampleFix":"// before\nfoo: String { host = \"x\" }\n// after\nfoo {\n  host = \"x\"\n}\n// or\nfoo: String = \"x\"","handlingStrategy":"validation","validationCode":"/^\\s*\\w+\\s*:\\s*[A-Z][\\w.]*(\\s*\\{)/.test(source) // flags typed property followed by '{' — refactor before parsing","typeGuard":null,"tryCatchPattern":"try { parser.parse(src) } catch (e) {\n  if (String(e.message).includes('type annotations cannot have object bodies')) {\n    throw new SyntaxError('Remove the type annotation or the { ... } body on the class property');\n  }\n  throw e;\n}","preventionTips":["Pick one style per property: typed default value OR amends body","Use an IDE with Pkl syntax highlighting","Lint templates that mix typed and amends syntax"],"tags":["parser","pkl","syntax-error","type-annotation","amends"],"backgroundTag":"invalid-argument-format","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"}