Skip to content

Commit ae8f5c7

Browse files
committed
fix(36883): disallow this parameter in set/get accessors
1 parent 4a34294 commit ae8f5c7

File tree

4 files changed

+77
-1
lines changed

4 files changed

+77
-1
lines changed

src/compiler/checker.ts

+3
Original file line numberDiff line numberDiff line change
@@ -28545,6 +28545,9 @@ namespace ts {
2854528545
if (func.kind === SyntaxKind.ArrowFunction) {
2854628546
error(node, Diagnostics.An_arrow_function_cannot_have_a_this_parameter);
2854728547
}
28548+
if (func.kind === SyntaxKind.GetAccessor || func.kind === SyntaxKind.SetAccessor) {
28549+
error(node, Diagnostics.get_and_set_accessors_cannot_declare_this_parameters);
28550+
}
2854828551
}
2854928552

2855028553
// Only check rest parameter type if it's not a binding pattern. Since binding patterns are

src/compiler/diagnosticMessages.json

+4
Original file line numberDiff line numberDiff line change
@@ -2891,6 +2891,10 @@
28912891
"category": "Error",
28922892
"code": 2783
28932893
},
2894+
"'get' and 'set' accessors cannot declare 'this' parameters.": {
2895+
"category": "Error",
2896+
"code": 2784
2897+
},
28942898

28952899
"Import declaration '{0}' is using private name '{1}'.": {
28962900
"category": "Error",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
tests/cases/conformance/types/thisType/thisTypeInAccessors.ts(8,11): error TS2784: 'get' and 'set' accessors cannot declare 'this' parameters.
2+
tests/cases/conformance/types/thisType/thisTypeInAccessors.ts(9,11): error TS2784: 'get' and 'set' accessors cannot declare 'this' parameters.
3+
tests/cases/conformance/types/thisType/thisTypeInAccessors.ts(13,11): error TS2784: 'get' and 'set' accessors cannot declare 'this' parameters.
4+
tests/cases/conformance/types/thisType/thisTypeInAccessors.ts(19,11): error TS2784: 'get' and 'set' accessors cannot declare 'this' parameters.
5+
tests/cases/conformance/types/thisType/thisTypeInAccessors.ts(23,11): error TS2784: 'get' and 'set' accessors cannot declare 'this' parameters.
6+
tests/cases/conformance/types/thisType/thisTypeInAccessors.ts(24,11): error TS2784: 'get' and 'set' accessors cannot declare 'this' parameters.
7+
tests/cases/conformance/types/thisType/thisTypeInAccessors.ts(29,11): error TS2784: 'get' and 'set' accessors cannot declare 'this' parameters.
8+
tests/cases/conformance/types/thisType/thisTypeInAccessors.ts(30,11): error TS2784: 'get' and 'set' accessors cannot declare 'this' parameters.
9+
10+
11+
==== tests/cases/conformance/types/thisType/thisTypeInAccessors.ts (8 errors) ====
12+
interface Foo {
13+
n: number;
14+
x: number;
15+
}
16+
17+
const explicit = {
18+
n: 12,
19+
get x(this: Foo): number { return this.n; },
20+
~~~~~~~~~
21+
!!! error TS2784: 'get' and 'set' accessors cannot declare 'this' parameters.
22+
set x(this: Foo, n: number) { this.n = n; }
23+
~~~~~~~~~
24+
!!! error TS2784: 'get' and 'set' accessors cannot declare 'this' parameters.
25+
}
26+
const copiedFromGetter = {
27+
n: 14,
28+
get x(this: Foo): number { return this.n; },
29+
~~~~~~~~~
30+
!!! error TS2784: 'get' and 'set' accessors cannot declare 'this' parameters.
31+
set x(n) { this.n = n; }
32+
}
33+
const copiedFromSetter = {
34+
n: 15,
35+
get x() { return this.n },
36+
set x(this: Foo, n: number) { this.n = n; }
37+
~~~~~~~~~
38+
!!! error TS2784: 'get' and 'set' accessors cannot declare 'this' parameters.
39+
}
40+
const copiedFromGetterUnannotated = {
41+
n: 16,
42+
get x(this: Foo) { return this.n },
43+
~~~~~~~~~
44+
!!! error TS2784: 'get' and 'set' accessors cannot declare 'this' parameters.
45+
set x(this, n) { this.n = n; }
46+
~~~~
47+
!!! error TS2784: 'get' and 'set' accessors cannot declare 'this' parameters.
48+
}
49+
50+
class Explicit {
51+
n = 17;
52+
get x(this: Foo): number { return this.n; }
53+
~~~~~~~~~
54+
!!! error TS2784: 'get' and 'set' accessors cannot declare 'this' parameters.
55+
set x(this: Foo, n: number) { this.n = n; }
56+
~~~~~~~~~
57+
!!! error TS2784: 'get' and 'set' accessors cannot declare 'this' parameters.
58+
}
59+
class Contextual {
60+
n = 21;
61+
get x() { return this.n } // inside a class, so already correct
62+
}
63+

tests/baselines/reference/thisTypeInAccessorsNegative.errors.txt

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
tests/cases/conformance/types/thisType/thisTypeInAccessorsNegative.ts(10,9): error TS2682: 'get' and 'set' accessor must have the same 'this' type.
2+
tests/cases/conformance/types/thisType/thisTypeInAccessorsNegative.ts(10,11): error TS2784: 'get' and 'set' accessors cannot declare 'this' parameters.
23
tests/cases/conformance/types/thisType/thisTypeInAccessorsNegative.ts(11,9): error TS2682: 'get' and 'set' accessor must have the same 'this' type.
4+
tests/cases/conformance/types/thisType/thisTypeInAccessorsNegative.ts(11,11): error TS2784: 'get' and 'set' accessors cannot declare 'this' parameters.
35

46

5-
==== tests/cases/conformance/types/thisType/thisTypeInAccessorsNegative.ts (2 errors) ====
7+
==== tests/cases/conformance/types/thisType/thisTypeInAccessorsNegative.ts (4 errors) ====
68
interface Foo {
79
n: number;
810
x: number;
@@ -15,9 +17,13 @@ tests/cases/conformance/types/thisType/thisTypeInAccessorsNegative.ts(11,9): err
1517
get x(this: Foo) { return this.n; },
1618
~
1719
!!! error TS2682: 'get' and 'set' accessor must have the same 'this' type.
20+
~~~~~~~~~
21+
!!! error TS2784: 'get' and 'set' accessors cannot declare 'this' parameters.
1822
set x(this: Bar, n) { this.wrong = "method"; }
1923
~
2024
!!! error TS2682: 'get' and 'set' accessor must have the same 'this' type.
25+
~~~~~~~~~
26+
!!! error TS2784: 'get' and 'set' accessors cannot declare 'this' parameters.
2127
}
2228
const contextual: Foo = {
2329
n: 16,

0 commit comments

Comments
 (0)