- Error
- Reported by the lexer and parser
TS1068
Unexpected token. A constructor, method, accessor, or property was expected.
The parser found a token inside a class body that cannot start a member. Remove the stray token or move statements into a method.
What it means
A class body may only contain members: a constructor, methods, accessors, properties, index signatures and static blocks. Anything else, such as a stray , or =>, is reported here. Commas between members are a common cause, since members are separated by ; or new lines, not commas.
Example
export class Counter {
count = 0;
,
}
ts-r-check -p tsconfig.json --pretty false ts-r outputindex.ts(3,3): error TS1068: Unexpected token. A constructor, method, accessor, or property was expected.
Recorded by node scripts/verify-examples.mjs with ts-r built from commit 6deaf01. The example uses the default tsconfig: strict, target: es2022, module: esnext, moduleResolution: bundler.
How to fix
- Remove the stray token.
- Separate members with semicolons or line breaks, not commas.
- Move statements (calls,
if, loops) into a method, the constructor or astaticblock.