{"record":{"id":"a5ef9f04713487e9","repo":"apple/pkl","slug":"typeannotationinamends-a5ef9f","errorCode":"typeAnnotationInAmends","errorMessage":"Properties with type annotations cannot have object bodies.\n\nTo define both a type annotation and an object body, try using assignment instead.\nFor example: `obj: Bird = new { ... }`.","messagePattern":"Properties with type annotations cannot have object bodies\\.\n\nTo define both a type annotation and an object body, try using assignment instead\\.\nFor example: `obj: Bird = new (.+?)`\\.","errorType":"exception","errorClass":"ParserError","httpStatus":null,"severity":"error","filePath":"pkl-parser/src/main/java/org/pkl/parser/ParserImpl.java","lineNumber":462,"sourceCode":"    var start = header.span(name.span());\n    var children = new ArrayList<@Nullable Node>();\n    children.add(header.docComment);\n    children.addAll(header.annotations);\n    var modifiersOffset = header.annotations.size() + 1;\n    children.addAll(header.modifiers);\n    var nameOffset = modifiersOffset + header.modifiers.size();\n    TypeAnnotation typeAnnotation = null;\n    Expr expr = null;\n    var bodies = new ArrayList<ObjectBody>();\n    if (lookahead == Token.COLON) {\n      typeAnnotation = parseTypeAnnotation();\n    }\n    if (lookahead == Token.ASSIGN) {\n      next();\n      expr = parseExpr();\n    } else if (lookahead == Token.LBRACE) {\n      if (typeAnnotation != null) {\n        throw parserError(\"typeAnnotationInAmends\");\n      }\n      while (lookahead == Token.LBRACE) {\n        bodies.add(parseObjectBody());\n      }\n    }\n    children.add(name);\n    children.add(typeAnnotation);\n    children.add(expr);\n    children.addAll(bodies);\n    if (expr != null) {\n      return new ClassProperty(children, modifiersOffset, nameOffset, start.endWith(expr.span()));\n    }\n    if (!bodies.isEmpty()) {\n      return new ClassProperty(\n          children,\n          modifiersOffset,\n          nameOffset,\n          start.endWith(bodies.get(bodies.size() - 1).span()));","sourceCodeStart":444,"sourceCodeEnd":480,"githubUrl":"https://github.com/apple/pkl/blob/f3efcbfc9b60d30053b0536d664948d7aa1b8673/pkl-parser/src/main/java/org/pkl/parser/ParserImpl.java#L444-L480","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Add an explicit assignment: use `obj: Bird = new { ... }` when constructing a new value, per the error hint.","If the intent is to AMEND an inherited value, write `obj: Bird = super { ... }` (or drop the annotation if the type is already known).","If no type annotation is needed, remove `: Type` and keep the plain `obj { ... }` amend form.","Only when the property has no `=` assignment may `{ ... }` bodies appear — so restructure to satisfy one of those two shapes."],"exampleFix":"// before\nclass Keeper {\n  bird: Bird { name = \"tweety\" }\n}\n// after\nclass Keeper {\n  bird: Bird = new { name = \"tweety\" }\n}","handlingStrategy":"validation","validationCode":"// Detect 'prop: Type {' shape before parsing and suggest the fix\nconst TYPED_AMEND = /^\\s*[A-Za-z_]\\w*\\s*:\\s*[A-Z][\\w.<>]\\w*(\\s*\\{)/;\n// if (TYPED_AMEND.test(line)) suggest(line.replace(/\\{/, '= new {'));","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Remember the rule: type annotation ⇒ assignment; `{ }` body ⇒ no annotation.","Write `prop: Type = new { ... }` for typed fresh objects.","Use plain `prop { ... }` when the type is inherited/inferred."],"tags":["parser","type-annotation","property","object-body"],"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"}