Skip to content

Commit 21eb38b

Browse files
Fix for crash when using ca call expression on private identifier coming from an any typed variable. (GH #42860)
1 parent 05ee94f commit 21eb38b

File tree

6 files changed

+145
-2
lines changed

6 files changed

+145
-2
lines changed

src/compiler/checker.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -21978,7 +21978,16 @@ namespace ts {
2197821978
const type = getTypeOfDottedName((<PropertyAccessExpression>node).expression, diagnostic);
2197921979
if (type) {
2198021980
const name = (<PropertyAccessExpression>node).name;
21981-
const prop = getPropertyOfType(type, isPrivateIdentifier(name) ? getSymbolNameForPrivateIdentifier(type.symbol, name.escapedText) : name.escapedText);
21981+
let prop: Symbol | undefined;
21982+
if (isPrivateIdentifier(name)) {
21983+
if (!type.symbol) {
21984+
return undefined;
21985+
}
21986+
prop = getPropertyOfType(type, getSymbolNameForPrivateIdentifier(type.symbol, name.escapedText));
21987+
}
21988+
else {
21989+
prop = getPropertyOfType(type, name.escapedText);
21990+
}
2198221991
return prop && getExplicitTypeOfSymbol(prop, diagnostic);
2198321992
}
2198421993
return undefined;
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,46 @@
11
tests/cases/conformance/classes/members/privateNames/privateNameAndAny.ts(5,15): error TS2339: Property '#bar' does not exist on type 'any'.
2+
tests/cases/conformance/classes/members/privateNames/privateNameAndAny.ts(9,9): error TS2571: Object is of type 'unknown'.
3+
tests/cases/conformance/classes/members/privateNames/privateNameAndAny.ts(10,9): error TS2571: Object is of type 'unknown'.
4+
tests/cases/conformance/classes/members/privateNames/privateNameAndAny.ts(10,15): error TS2339: Property '#bar' does not exist on type 'any'.
5+
tests/cases/conformance/classes/members/privateNames/privateNameAndAny.ts(11,9): error TS2571: Object is of type 'unknown'.
6+
tests/cases/conformance/classes/members/privateNames/privateNameAndAny.ts(14,15): error TS2339: Property '#foo' does not exist on type 'never'.
7+
tests/cases/conformance/classes/members/privateNames/privateNameAndAny.ts(15,15): error TS2339: Property '#bar' does not exist on type 'never'.
8+
tests/cases/conformance/classes/members/privateNames/privateNameAndAny.ts(16,15): error TS2339: Property '#foo' does not exist on type 'never'.
29

310

4-
==== tests/cases/conformance/classes/members/privateNames/privateNameAndAny.ts (1 errors) ====
11+
==== tests/cases/conformance/classes/members/privateNames/privateNameAndAny.ts (8 errors) ====
512
class A {
613
#foo = true;
714
method(thing: any) {
815
thing.#foo; // OK
916
thing.#bar; // Error
1017
~~~~
1118
!!! error TS2339: Property '#bar' does not exist on type 'any'.
19+
thing.#foo();
20+
}
21+
methodU(thing: unknown) {
22+
thing.#foo;
23+
~~~~~
24+
!!! error TS2571: Object is of type 'unknown'.
25+
thing.#bar;
26+
~~~~~
27+
!!! error TS2571: Object is of type 'unknown'.
28+
~~~~
29+
!!! error TS2339: Property '#bar' does not exist on type 'any'.
30+
thing.#foo();
31+
~~~~~
32+
!!! error TS2571: Object is of type 'unknown'.
33+
}
34+
methodN(thing: never) {
35+
thing.#foo;
36+
~~~~
37+
!!! error TS2339: Property '#foo' does not exist on type 'never'.
38+
thing.#bar;
39+
~~~~
40+
!!! error TS2339: Property '#bar' does not exist on type 'never'.
41+
thing.#foo();
42+
~~~~
43+
!!! error TS2339: Property '#foo' does not exist on type 'never'.
1244
}
1345
};
1446

tests/baselines/reference/privateNameAndAny.js

+23
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,17 @@ class A {
44
method(thing: any) {
55
thing.#foo; // OK
66
thing.#bar; // Error
7+
thing.#foo();
8+
}
9+
methodU(thing: unknown) {
10+
thing.#foo;
11+
thing.#bar;
12+
thing.#foo();
13+
}
14+
methodN(thing: never) {
15+
thing.#foo;
16+
thing.#bar;
17+
thing.#foo();
718
}
819
};
920

@@ -24,6 +35,18 @@ class A {
2435
method(thing) {
2536
__classPrivateFieldGet(thing, _foo); // OK
2637
thing.; // Error
38+
__classPrivateFieldGet(thing, _foo).call(// Error
39+
thing);
40+
}
41+
methodU(thing) {
42+
__classPrivateFieldGet(thing, _foo);
43+
thing.;
44+
__classPrivateFieldGet(thing, _foo).call(thing);
45+
}
46+
methodN(thing) {
47+
__classPrivateFieldGet(thing, _foo);
48+
thing.;
49+
__classPrivateFieldGet(thing, _foo).call(thing);
2750
}
2851
}
2952
_foo = new WeakMap();

tests/baselines/reference/privateNameAndAny.symbols

+29
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,35 @@ class A {
1414

1515
thing.#bar; // Error
1616
>thing : Symbol(thing, Decl(privateNameAndAny.ts, 2, 11))
17+
18+
thing.#foo();
19+
>thing : Symbol(thing, Decl(privateNameAndAny.ts, 2, 11))
20+
}
21+
methodU(thing: unknown) {
22+
>methodU : Symbol(A.methodU, Decl(privateNameAndAny.ts, 6, 5))
23+
>thing : Symbol(thing, Decl(privateNameAndAny.ts, 7, 12))
24+
25+
thing.#foo;
26+
>thing : Symbol(thing, Decl(privateNameAndAny.ts, 7, 12))
27+
28+
thing.#bar;
29+
>thing : Symbol(thing, Decl(privateNameAndAny.ts, 7, 12))
30+
31+
thing.#foo();
32+
>thing : Symbol(thing, Decl(privateNameAndAny.ts, 7, 12))
33+
}
34+
methodN(thing: never) {
35+
>methodN : Symbol(A.methodN, Decl(privateNameAndAny.ts, 11, 5))
36+
>thing : Symbol(thing, Decl(privateNameAndAny.ts, 12, 12))
37+
38+
thing.#foo;
39+
>thing : Symbol(thing, Decl(privateNameAndAny.ts, 12, 12))
40+
41+
thing.#bar;
42+
>thing : Symbol(thing, Decl(privateNameAndAny.ts, 12, 12))
43+
44+
thing.#foo();
45+
>thing : Symbol(thing, Decl(privateNameAndAny.ts, 12, 12))
1746
}
1847
};
1948

tests/baselines/reference/privateNameAndAny.types

+39
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,45 @@ class A {
1717
thing.#bar; // Error
1818
>thing.#bar : any
1919
>thing : any
20+
21+
thing.#foo();
22+
>thing.#foo() : any
23+
>thing.#foo : any
24+
>thing : any
25+
}
26+
methodU(thing: unknown) {
27+
>methodU : (thing: unknown) => void
28+
>thing : unknown
29+
30+
thing.#foo;
31+
>thing.#foo : any
32+
>thing : unknown
33+
34+
thing.#bar;
35+
>thing.#bar : any
36+
>thing : unknown
37+
38+
thing.#foo();
39+
>thing.#foo() : any
40+
>thing.#foo : any
41+
>thing : unknown
42+
}
43+
methodN(thing: never) {
44+
>methodN : (thing: never) => void
45+
>thing : never
46+
47+
thing.#foo;
48+
>thing.#foo : any
49+
>thing : never
50+
51+
thing.#bar;
52+
>thing.#bar : any
53+
>thing : never
54+
55+
thing.#foo();
56+
>thing.#foo() : any
57+
>thing.#foo : any
58+
>thing : never
2059
}
2160
};
2261

tests/cases/conformance/classes/members/privateNames/privateNameAndAny.ts

+11
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,16 @@ class A {
66
method(thing: any) {
77
thing.#foo; // OK
88
thing.#bar; // Error
9+
thing.#foo();
10+
}
11+
methodU(thing: unknown) {
12+
thing.#foo;
13+
thing.#bar;
14+
thing.#foo();
15+
}
16+
methodN(thing: never) {
17+
thing.#foo;
18+
thing.#bar;
19+
thing.#foo();
920
}
1021
};

0 commit comments

Comments
 (0)