mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-20 13:45:34 -05:00
fix(42166): allow assertion signature for private identifiers (#42176)
This commit is contained in:
@@ -21852,10 +21852,15 @@ namespace ts {
|
||||
return getExplicitThisType(node);
|
||||
case SyntaxKind.SuperKeyword:
|
||||
return checkSuperExpression(node);
|
||||
case SyntaxKind.PropertyAccessExpression:
|
||||
case SyntaxKind.PropertyAccessExpression: {
|
||||
const type = getTypeOfDottedName((<PropertyAccessExpression>node).expression, diagnostic);
|
||||
const prop = type && getPropertyOfType(type, (<PropertyAccessExpression>node).name.escapedText);
|
||||
return prop && getExplicitTypeOfSymbol(prop, diagnostic);
|
||||
if (type) {
|
||||
const name = (<PropertyAccessExpression>node).name;
|
||||
const prop = getPropertyOfType(type, isPrivateIdentifier(name) ? getSymbolNameForPrivateIdentifier(type.symbol, name.escapedText) : name.escapedText);
|
||||
return prop && getExplicitTypeOfSymbol(prop, diagnostic);
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
case SyntaxKind.ParenthesizedExpression:
|
||||
return getTypeOfDottedName((<ParenthesizedExpression>node).expression, diagnostic);
|
||||
}
|
||||
|
||||
30
tests/baselines/reference/privateNamesAssertion.js
Normal file
30
tests/baselines/reference/privateNamesAssertion.js
Normal file
@@ -0,0 +1,30 @@
|
||||
//// [privateNamesAssertion.ts]
|
||||
class Foo {
|
||||
#p1: (v: any) => asserts v is string = (v) => {
|
||||
if (typeof v !== "string") {
|
||||
throw new Error();
|
||||
}
|
||||
}
|
||||
m1(v: unknown) {
|
||||
this.#p1(v);
|
||||
v;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//// [privateNamesAssertion.js]
|
||||
"use strict";
|
||||
class Foo {
|
||||
constructor() {
|
||||
this.#p1 = (v) => {
|
||||
if (typeof v !== "string") {
|
||||
throw new Error();
|
||||
}
|
||||
};
|
||||
}
|
||||
#p1;
|
||||
m1(v) {
|
||||
this.#p1(v);
|
||||
v;
|
||||
}
|
||||
}
|
||||
31
tests/baselines/reference/privateNamesAssertion.symbols
Normal file
31
tests/baselines/reference/privateNamesAssertion.symbols
Normal file
@@ -0,0 +1,31 @@
|
||||
=== tests/cases/conformance/classes/members/privateNames/privateNamesAssertion.ts ===
|
||||
class Foo {
|
||||
>Foo : Symbol(Foo, Decl(privateNamesAssertion.ts, 0, 0))
|
||||
|
||||
#p1: (v: any) => asserts v is string = (v) => {
|
||||
>#p1 : Symbol(Foo.#p1, Decl(privateNamesAssertion.ts, 0, 11))
|
||||
>v : Symbol(v, Decl(privateNamesAssertion.ts, 1, 10))
|
||||
>v : Symbol(v, Decl(privateNamesAssertion.ts, 1, 10))
|
||||
>v : Symbol(v, Decl(privateNamesAssertion.ts, 1, 44))
|
||||
|
||||
if (typeof v !== "string") {
|
||||
>v : Symbol(v, Decl(privateNamesAssertion.ts, 1, 44))
|
||||
|
||||
throw new Error();
|
||||
>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
|
||||
}
|
||||
}
|
||||
m1(v: unknown) {
|
||||
>m1 : Symbol(Foo.m1, Decl(privateNamesAssertion.ts, 5, 5))
|
||||
>v : Symbol(v, Decl(privateNamesAssertion.ts, 6, 7))
|
||||
|
||||
this.#p1(v);
|
||||
>this.#p1 : Symbol(Foo.#p1, Decl(privateNamesAssertion.ts, 0, 11))
|
||||
>this : Symbol(Foo, Decl(privateNamesAssertion.ts, 0, 0))
|
||||
>v : Symbol(v, Decl(privateNamesAssertion.ts, 6, 7))
|
||||
|
||||
v;
|
||||
>v : Symbol(v, Decl(privateNamesAssertion.ts, 6, 7))
|
||||
}
|
||||
}
|
||||
|
||||
36
tests/baselines/reference/privateNamesAssertion.types
Normal file
36
tests/baselines/reference/privateNamesAssertion.types
Normal file
@@ -0,0 +1,36 @@
|
||||
=== tests/cases/conformance/classes/members/privateNames/privateNamesAssertion.ts ===
|
||||
class Foo {
|
||||
>Foo : Foo
|
||||
|
||||
#p1: (v: any) => asserts v is string = (v) => {
|
||||
>#p1 : (v: any) => asserts v is string
|
||||
>v : any
|
||||
>(v) => { if (typeof v !== "string") { throw new Error(); } } : (v: any) => void
|
||||
>v : any
|
||||
|
||||
if (typeof v !== "string") {
|
||||
>typeof v !== "string" : boolean
|
||||
>typeof v : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
|
||||
>v : any
|
||||
>"string" : "string"
|
||||
|
||||
throw new Error();
|
||||
>new Error() : Error
|
||||
>Error : ErrorConstructor
|
||||
}
|
||||
}
|
||||
m1(v: unknown) {
|
||||
>m1 : (v: unknown) => void
|
||||
>v : unknown
|
||||
|
||||
this.#p1(v);
|
||||
>this.#p1(v) : void
|
||||
>this.#p1 : (v: any) => asserts v is string
|
||||
>this : this
|
||||
>v : unknown
|
||||
|
||||
v;
|
||||
>v : string
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
// @strict: true
|
||||
// @target: esnext
|
||||
|
||||
class Foo {
|
||||
#p1: (v: any) => asserts v is string = (v) => {
|
||||
if (typeof v !== "string") {
|
||||
throw new Error();
|
||||
}
|
||||
}
|
||||
m1(v: unknown) {
|
||||
this.#p1(v);
|
||||
v;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user