Closed
Description
π Search Terms
DecoratorCallExpression
π Version & Regression Information
- This issue is specific to TypeScript 5.0+ decorators (not experimental decorators)
β― Playground Link
π» Code
declare let x: () => { y: () => (x: any, y: any) => any }
@x().y()
class foo {}
π Actual behavior
TypeScript allows this invalid syntax without any errors.
π Expected behavior
Here's the grammar (taken from the spec linked in the proposal):
Decorator[Yield, Await] :
@ DecoratorMemberExpression[?Yield, ?Await]
@ DecoratorParenthesizedExpression[?Yield, ?Await]
@ DecoratorCallExpression[?Yield, ?Await]
DecoratorMemberExpression[Yield, Await] :
IdentifierReference[?Yield, ?Await]
DecoratorMemberExpression[?Yield, ?Await] . IdentifierName
DecoratorMemberExpression[?Yield, ?Await] . PrivateIdentifier
DecoratorParenthesizedExpression[Yield, Await] :
( Expression[+In, ?Yield, ?Await] )
DecoratorCallExpression[Yield, Await] :
DecoratorMemberExpression[?Yield, ?Await] Arguments[?Yield, ?Await]
My reading of this grammar is that TypeScript should only be allowing a single Arguments
production at the end of the decorator. So I believe TypeScript should consider this example to be a syntax error. Here's what other decorator parsers do:
-
/repl.ts: Leading decorators must be attached to a class declaration. (2:4) 1 | declare let x: () => { y: () => (x: any, y: any) => any } > 2 | @x().y() | ^ 3 | class foo {} 4 |
-
β [ERROR] Expected "class" after decorator but found "." <stdin>:2:4: 2 β @x().y() β΅ ^ The preceding decorator is here: <stdin>:2:0: 2 β @x().y() β΅ ^ Decorators can only be used with class declarations.