Skip to content

Commit 46d3caa

Browse files
author
Andy
authored
Don't error on destructure of private property with computed property syntax (microsoft#26360)
1 parent c8e10a9 commit 46d3caa

6 files changed

+50
-19
lines changed

src/compiler/checker.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -24389,7 +24389,7 @@ namespace ts {
2438924389
if (nameText) {
2439024390
const property = getPropertyOfType(parentType!, nameText)!; // TODO: GH#18217
2439124391
markPropertyAsReferenced(property, /*nodeForCheckWriteOnly*/ undefined, /*isThisAccess*/ false); // A destructuring is never a write-only reference.
24392-
if (parent.initializer && property) {
24392+
if (parent.initializer && property && !isComputedPropertyName(name)) {
2439324393
checkPropertyAccessibility(parent, parent.initializer, parentType!, property);
2439424394
}
2439524395
}
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
1-
tests/cases/compiler/destructureComputedProperty.ts(8,7): error TS2341: Property 'p' is private and only accessible within class 'C'.
1+
tests/cases/compiler/destructureComputedProperty.ts(7,7): error TS2341: Property 'p' is private and only accessible within class 'C'.
2+
tests/cases/compiler/destructureComputedProperty.ts(10,7): error TS2341: Property 'p' is private and only accessible within class 'C'.
23

34

4-
==== tests/cases/compiler/destructureComputedProperty.ts (1 errors) ====
5+
==== tests/cases/compiler/destructureComputedProperty.ts (2 errors) ====
56
declare const ab: { n: number } | { n: string };
67
const nameN = "n";
78
const { [nameN]: n } = ab;
89

910
class C { private p: number; }
1011
const nameP = "p";
11-
const { [nameP]: p } = new C();
12-
const { p: p2 } = new C();
12+
const { "p": p0 } = new C();
13+
~~~~~~~~~~~
14+
!!! error TS2341: Property 'p' is private and only accessible within class 'C'.
15+
const { ["p"]: p1 } = new C();
16+
const { [nameP]: p2 } = new C();
17+
const { p: p3 } = new C();
1318
~~~~~~~~~
1419
!!! error TS2341: Property 'p' is private and only accessible within class 'C'.
1520

tests/baselines/reference/destructureComputedProperty.js

+8-4
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ const { [nameN]: n } = ab;
55

66
class C { private p: number; }
77
const nameP = "p";
8-
const { [nameP]: p } = new C();
9-
const { p: p2 } = new C();
8+
const { "p": p0 } = new C();
9+
const { ["p"]: p1 } = new C();
10+
const { [nameP]: p2 } = new C();
11+
const { p: p3 } = new C();
1012

1113

1214
//// [destructureComputedProperty.js]
@@ -18,5 +20,7 @@ var C = /** @class */ (function () {
1820
return C;
1921
}());
2022
var nameP = "p";
21-
var _b = nameP, p = new C()[_b];
22-
var p2 = new C().p;
23+
var p0 = new C()["p"];
24+
var p1 = new C()["p"];
25+
var _b = nameP, p2 = new C()[_b];
26+
var p3 = new C().p;

tests/baselines/reference/destructureComputedProperty.symbols

+13-4
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,22 @@ class C { private p: number; }
1919
const nameP = "p";
2020
>nameP : Symbol(nameP, Decl(destructureComputedProperty.ts, 5, 5))
2121

22-
const { [nameP]: p } = new C();
22+
const { "p": p0 } = new C();
23+
>p0 : Symbol(p0, Decl(destructureComputedProperty.ts, 6, 7))
24+
>C : Symbol(C, Decl(destructureComputedProperty.ts, 2, 26))
25+
26+
const { ["p"]: p1 } = new C();
27+
>"p" : Symbol(p1, Decl(destructureComputedProperty.ts, 7, 7))
28+
>p1 : Symbol(p1, Decl(destructureComputedProperty.ts, 7, 7))
29+
>C : Symbol(C, Decl(destructureComputedProperty.ts, 2, 26))
30+
31+
const { [nameP]: p2 } = new C();
2332
>nameP : Symbol(nameP, Decl(destructureComputedProperty.ts, 5, 5))
24-
>p : Symbol(p, Decl(destructureComputedProperty.ts, 6, 7))
33+
>p2 : Symbol(p2, Decl(destructureComputedProperty.ts, 8, 7))
2534
>C : Symbol(C, Decl(destructureComputedProperty.ts, 2, 26))
2635

27-
const { p: p2 } = new C();
36+
const { p: p3 } = new C();
2837
>p : Symbol(C.p, Decl(destructureComputedProperty.ts, 4, 9))
29-
>p2 : Symbol(p2, Decl(destructureComputedProperty.ts, 7, 7))
38+
>p3 : Symbol(p3, Decl(destructureComputedProperty.ts, 9, 7))
3039
>C : Symbol(C, Decl(destructureComputedProperty.ts, 2, 26))
3140

tests/baselines/reference/destructureComputedProperty.types

+15-4
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,26 @@ const nameP = "p";
2121
>nameP : "p"
2222
>"p" : "p"
2323

24-
const { [nameP]: p } = new C();
24+
const { "p": p0 } = new C();
25+
>p0 : number
26+
>new C() : C
27+
>C : typeof C
28+
29+
const { ["p"]: p1 } = new C();
30+
>"p" : "p"
31+
>p1 : number
32+
>new C() : C
33+
>C : typeof C
34+
35+
const { [nameP]: p2 } = new C();
2536
>nameP : "p"
26-
>p : number
37+
>p2 : number
2738
>new C() : C
2839
>C : typeof C
2940

30-
const { p: p2 } = new C();
41+
const { p: p3 } = new C();
3142
>p : any
32-
>p2 : number
43+
>p3 : number
3344
>new C() : C
3445
>C : typeof C
3546

tests/cases/compiler/destructureComputedProperty.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,7 @@ const { [nameN]: n } = ab;
44

55
class C { private p: number; }
66
const nameP = "p";
7-
const { [nameP]: p } = new C();
8-
const { p: p2 } = new C();
7+
const { "p": p0 } = new C();
8+
const { ["p"]: p1 } = new C();
9+
const { [nameP]: p2 } = new C();
10+
const { p: p3 } = new C();

0 commit comments

Comments
 (0)