fix(36883): accessor allows this parameter but is not checke… (#36889)

This commit is contained in:
Alexander T
2020-03-03 22:26:41 +02:00
committed by GitHub
parent 176241cca9
commit 3046a54401
4 changed files with 77 additions and 1 deletions

View File

@@ -28787,6 +28787,9 @@ namespace ts {
if (func.kind === SyntaxKind.ArrowFunction) {
error(node, Diagnostics.An_arrow_function_cannot_have_a_this_parameter);
}
if (func.kind === SyntaxKind.GetAccessor || func.kind === SyntaxKind.SetAccessor) {
error(node, Diagnostics.get_and_set_accessors_cannot_declare_this_parameters);
}
}
// Only check rest parameter type if it's not a binding pattern. Since binding patterns are

View File

@@ -2899,6 +2899,10 @@
"category": "Error",
"code": 2783
},
"'get' and 'set' accessors cannot declare 'this' parameters.": {
"category": "Error",
"code": 2784
},
"Import declaration '{0}' is using private name '{1}'.": {
"category": "Error",

View File

@@ -0,0 +1,63 @@
tests/cases/conformance/types/thisType/thisTypeInAccessors.ts(8,11): error TS2784: 'get' and 'set' accessors cannot declare 'this' parameters.
tests/cases/conformance/types/thisType/thisTypeInAccessors.ts(9,11): error TS2784: 'get' and 'set' accessors cannot declare 'this' parameters.
tests/cases/conformance/types/thisType/thisTypeInAccessors.ts(13,11): error TS2784: 'get' and 'set' accessors cannot declare 'this' parameters.
tests/cases/conformance/types/thisType/thisTypeInAccessors.ts(19,11): error TS2784: 'get' and 'set' accessors cannot declare 'this' parameters.
tests/cases/conformance/types/thisType/thisTypeInAccessors.ts(23,11): error TS2784: 'get' and 'set' accessors cannot declare 'this' parameters.
tests/cases/conformance/types/thisType/thisTypeInAccessors.ts(24,11): error TS2784: 'get' and 'set' accessors cannot declare 'this' parameters.
tests/cases/conformance/types/thisType/thisTypeInAccessors.ts(29,11): error TS2784: 'get' and 'set' accessors cannot declare 'this' parameters.
tests/cases/conformance/types/thisType/thisTypeInAccessors.ts(30,11): error TS2784: 'get' and 'set' accessors cannot declare 'this' parameters.
==== tests/cases/conformance/types/thisType/thisTypeInAccessors.ts (8 errors) ====
interface Foo {
n: number;
x: number;
}
const explicit = {
n: 12,
get x(this: Foo): number { return this.n; },
~~~~~~~~~
!!! error TS2784: 'get' and 'set' accessors cannot declare 'this' parameters.
set x(this: Foo, n: number) { this.n = n; }
~~~~~~~~~
!!! error TS2784: 'get' and 'set' accessors cannot declare 'this' parameters.
}
const copiedFromGetter = {
n: 14,
get x(this: Foo): number { return this.n; },
~~~~~~~~~
!!! error TS2784: 'get' and 'set' accessors cannot declare 'this' parameters.
set x(n) { this.n = n; }
}
const copiedFromSetter = {
n: 15,
get x() { return this.n },
set x(this: Foo, n: number) { this.n = n; }
~~~~~~~~~
!!! error TS2784: 'get' and 'set' accessors cannot declare 'this' parameters.
}
const copiedFromGetterUnannotated = {
n: 16,
get x(this: Foo) { return this.n },
~~~~~~~~~
!!! error TS2784: 'get' and 'set' accessors cannot declare 'this' parameters.
set x(this, n) { this.n = n; }
~~~~
!!! error TS2784: 'get' and 'set' accessors cannot declare 'this' parameters.
}
class Explicit {
n = 17;
get x(this: Foo): number { return this.n; }
~~~~~~~~~
!!! error TS2784: 'get' and 'set' accessors cannot declare 'this' parameters.
set x(this: Foo, n: number) { this.n = n; }
~~~~~~~~~
!!! error TS2784: 'get' and 'set' accessors cannot declare 'this' parameters.
}
class Contextual {
n = 21;
get x() { return this.n } // inside a class, so already correct
}

View File

@@ -1,8 +1,10 @@
tests/cases/conformance/types/thisType/thisTypeInAccessorsNegative.ts(10,9): error TS2682: 'get' and 'set' accessor must have the same 'this' type.
tests/cases/conformance/types/thisType/thisTypeInAccessorsNegative.ts(10,11): error TS2784: 'get' and 'set' accessors cannot declare 'this' parameters.
tests/cases/conformance/types/thisType/thisTypeInAccessorsNegative.ts(11,9): error TS2682: 'get' and 'set' accessor must have the same 'this' type.
tests/cases/conformance/types/thisType/thisTypeInAccessorsNegative.ts(11,11): error TS2784: 'get' and 'set' accessors cannot declare 'this' parameters.
==== tests/cases/conformance/types/thisType/thisTypeInAccessorsNegative.ts (2 errors) ====
==== tests/cases/conformance/types/thisType/thisTypeInAccessorsNegative.ts (4 errors) ====
interface Foo {
n: number;
x: number;
@@ -15,9 +17,13 @@ tests/cases/conformance/types/thisType/thisTypeInAccessorsNegative.ts(11,9): err
get x(this: Foo) { return this.n; },
~
!!! error TS2682: 'get' and 'set' accessor must have the same 'this' type.
~~~~~~~~~
!!! error TS2784: 'get' and 'set' accessors cannot declare 'this' parameters.
set x(this: Bar, n) { this.wrong = "method"; }
~
!!! error TS2682: 'get' and 'set' accessor must have the same 'this' type.
~~~~~~~~~
!!! error TS2784: 'get' and 'set' accessors cannot declare 'this' parameters.
}
const contextual: Foo = {
n: 16,