Skip to content

Commit 1ecf228

Browse files
authored
fix(42166): allow assertion signature for private identifiers (#42176)
1 parent 8ddea6b commit 1ecf228

File tree

5 files changed

+119
-3
lines changed

5 files changed

+119
-3
lines changed

src/compiler/checker.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21852,10 +21852,15 @@ namespace ts {
2185221852
return getExplicitThisType(node);
2185321853
case SyntaxKind.SuperKeyword:
2185421854
return checkSuperExpression(node);
21855-
case SyntaxKind.PropertyAccessExpression:
21855+
case SyntaxKind.PropertyAccessExpression: {
2185621856
const type = getTypeOfDottedName((<PropertyAccessExpression>node).expression, diagnostic);
21857-
const prop = type && getPropertyOfType(type, (<PropertyAccessExpression>node).name.escapedText);
21858-
return prop && getExplicitTypeOfSymbol(prop, diagnostic);
21857+
if (type) {
21858+
const name = (<PropertyAccessExpression>node).name;
21859+
const prop = getPropertyOfType(type, isPrivateIdentifier(name) ? getSymbolNameForPrivateIdentifier(type.symbol, name.escapedText) : name.escapedText);
21860+
return prop && getExplicitTypeOfSymbol(prop, diagnostic);
21861+
}
21862+
return undefined;
21863+
}
2185921864
case SyntaxKind.ParenthesizedExpression:
2186021865
return getTypeOfDottedName((<ParenthesizedExpression>node).expression, diagnostic);
2186121866
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//// [privateNamesAssertion.ts]
2+
class Foo {
3+
#p1: (v: any) => asserts v is string = (v) => {
4+
if (typeof v !== "string") {
5+
throw new Error();
6+
}
7+
}
8+
m1(v: unknown) {
9+
this.#p1(v);
10+
v;
11+
}
12+
}
13+
14+
15+
//// [privateNamesAssertion.js]
16+
"use strict";
17+
class Foo {
18+
constructor() {
19+
this.#p1 = (v) => {
20+
if (typeof v !== "string") {
21+
throw new Error();
22+
}
23+
};
24+
}
25+
#p1;
26+
m1(v) {
27+
this.#p1(v);
28+
v;
29+
}
30+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
=== tests/cases/conformance/classes/members/privateNames/privateNamesAssertion.ts ===
2+
class Foo {
3+
>Foo : Symbol(Foo, Decl(privateNamesAssertion.ts, 0, 0))
4+
5+
#p1: (v: any) => asserts v is string = (v) => {
6+
>#p1 : Symbol(Foo.#p1, Decl(privateNamesAssertion.ts, 0, 11))
7+
>v : Symbol(v, Decl(privateNamesAssertion.ts, 1, 10))
8+
>v : Symbol(v, Decl(privateNamesAssertion.ts, 1, 10))
9+
>v : Symbol(v, Decl(privateNamesAssertion.ts, 1, 44))
10+
11+
if (typeof v !== "string") {
12+
>v : Symbol(v, Decl(privateNamesAssertion.ts, 1, 44))
13+
14+
throw new Error();
15+
>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
16+
}
17+
}
18+
m1(v: unknown) {
19+
>m1 : Symbol(Foo.m1, Decl(privateNamesAssertion.ts, 5, 5))
20+
>v : Symbol(v, Decl(privateNamesAssertion.ts, 6, 7))
21+
22+
this.#p1(v);
23+
>this.#p1 : Symbol(Foo.#p1, Decl(privateNamesAssertion.ts, 0, 11))
24+
>this : Symbol(Foo, Decl(privateNamesAssertion.ts, 0, 0))
25+
>v : Symbol(v, Decl(privateNamesAssertion.ts, 6, 7))
26+
27+
v;
28+
>v : Symbol(v, Decl(privateNamesAssertion.ts, 6, 7))
29+
}
30+
}
31+
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
=== tests/cases/conformance/classes/members/privateNames/privateNamesAssertion.ts ===
2+
class Foo {
3+
>Foo : Foo
4+
5+
#p1: (v: any) => asserts v is string = (v) => {
6+
>#p1 : (v: any) => asserts v is string
7+
>v : any
8+
>(v) => { if (typeof v !== "string") { throw new Error(); } } : (v: any) => void
9+
>v : any
10+
11+
if (typeof v !== "string") {
12+
>typeof v !== "string" : boolean
13+
>typeof v : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
14+
>v : any
15+
>"string" : "string"
16+
17+
throw new Error();
18+
>new Error() : Error
19+
>Error : ErrorConstructor
20+
}
21+
}
22+
m1(v: unknown) {
23+
>m1 : (v: unknown) => void
24+
>v : unknown
25+
26+
this.#p1(v);
27+
>this.#p1(v) : void
28+
>this.#p1 : (v: any) => asserts v is string
29+
>this : this
30+
>v : unknown
31+
32+
v;
33+
>v : string
34+
}
35+
}
36+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// @strict: true
2+
// @target: esnext
3+
4+
class Foo {
5+
#p1: (v: any) => asserts v is string = (v) => {
6+
if (typeof v !== "string") {
7+
throw new Error();
8+
}
9+
}
10+
m1(v: unknown) {
11+
this.#p1(v);
12+
v;
13+
}
14+
}

0 commit comments

Comments
 (0)