fix(42166): allow assertion signature for private identifiers (#42176)

This commit is contained in:
Oleksandr T
2021-01-11 19:25:51 +02:00
committed by GitHub
parent 8ddea6b7a6
commit 1ecf22884f
5 changed files with 119 additions and 3 deletions

View File

@@ -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);
}

View 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;
}
}

View 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))
}
}

View 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
}
}

View File

@@ -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;
}
}