- Error
- Reported by the lexer and parser
TS1497
Expression must be enclosed in parentheses to be used as a decorator.
A decorator expression is more than a name, property access or call. Wrap the expression in parentheses.
What it means
Decorators only accept a simple form: @name, @a.b.c or a call like @factory(). Anything else, such as an element access or a call on a computed value, must be put in parentheses.
Example
declare const registry:
| { service(target: unknown, context: ClassDecoratorContext): void }
| undefined;
@registry?.service
export class Service {}
ts-r-check -p tsconfig.json --pretty false ts-r outputindex.ts(5,2): error TS1497: Expression must be enclosed in parentheses to be used as a decorator.
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
- Wrap it:
@(registry?.service), or narrow first and use a plain name.