diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 35e719e7a7c..5d57e9d2028 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -15563,7 +15563,12 @@ namespace ts { return false; } if (!nodeCanBeDecorated(node)) { - return grammarErrorOnFirstToken(node, Diagnostics.Decorators_are_not_valid_here); + if (node.kind === SyntaxKind.MethodDeclaration && !ts.nodeIsPresent((node).body)) { + return grammarErrorOnFirstToken(node, Diagnostics.A_decorator_can_only_decorate_a_method_implementation_not_an_overload); + } + else { + return grammarErrorOnFirstToken(node, Diagnostics.Decorators_are_not_valid_here); + } } else if (node.kind === SyntaxKind.GetAccessor || node.kind === SyntaxKind.SetAccessor) { const accessors = getAllAccessorDeclarations((node.parent).members, node); diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index 1d1510e6eb6..a43f3534df6 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -795,6 +795,10 @@ "category": "Error", "code": 1248 }, + "A decorator can only decorate a method implementation, not an overload.": { + "category": "Error", + "code": 1249 + }, "'with' statements are not allowed in an async function block.": { "category": "Error", "code": 1300 diff --git a/tests/baselines/reference/decoratorOnClassMethodOverload1.errors.txt b/tests/baselines/reference/decoratorOnClassMethodOverload1.errors.txt new file mode 100644 index 00000000000..d1670bd6bb9 --- /dev/null +++ b/tests/baselines/reference/decoratorOnClassMethodOverload1.errors.txt @@ -0,0 +1,13 @@ +tests/cases/conformance/decorators/class/method/decoratorOnClassMethodOverload1.ts(4,5): error TS1249: A decorator can only decorate a method implementation, not an overload. + + +==== tests/cases/conformance/decorators/class/method/decoratorOnClassMethodOverload1.ts (1 errors) ==== + declare function dec(target: any, propertyKey: string, descriptor: TypedPropertyDescriptor): TypedPropertyDescriptor; + + class C { + @dec + ~ +!!! error TS1249: A decorator can only decorate a method implementation, not an overload. + method() + method() { } + } \ No newline at end of file diff --git a/tests/baselines/reference/decoratorOnClassMethodOverload1.js b/tests/baselines/reference/decoratorOnClassMethodOverload1.js new file mode 100644 index 00000000000..cb2e29b3d00 --- /dev/null +++ b/tests/baselines/reference/decoratorOnClassMethodOverload1.js @@ -0,0 +1,16 @@ +//// [decoratorOnClassMethodOverload1.ts] +declare function dec(target: any, propertyKey: string, descriptor: TypedPropertyDescriptor): TypedPropertyDescriptor; + +class C { + @dec + method() + method() { } +} + +//// [decoratorOnClassMethodOverload1.js] +var C = (function () { + function C() { + } + C.prototype.method = function () { }; + return C; +}()); diff --git a/tests/cases/conformance/decorators/class/method/decoratorOnClassMethodOverload1.ts b/tests/cases/conformance/decorators/class/method/decoratorOnClassMethodOverload1.ts new file mode 100644 index 00000000000..722463b33da --- /dev/null +++ b/tests/cases/conformance/decorators/class/method/decoratorOnClassMethodOverload1.ts @@ -0,0 +1,9 @@ +// @target: ES5 +// @experimentaldecorators: true +declare function dec(target: any, propertyKey: string, descriptor: TypedPropertyDescriptor): TypedPropertyDescriptor; + +class C { + @dec + method() + method() { } +} \ No newline at end of file