Skip to content

fix(42166): Assertion signature not working on private identifier #42176

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21655,10 +21655,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);
}
Expand Down
30 changes: 30 additions & 0 deletions tests/baselines/reference/privateNamesAssertion.js
Original file line number Diff line number Diff line change
@@ -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 changes: 31 additions & 0 deletions tests/baselines/reference/privateNamesAssertion.symbols
Original file line number Diff line number Diff line change
@@ -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 changes: 36 additions & 0 deletions tests/baselines/reference/privateNamesAssertion.types
Original file line number Diff line number Diff line change
@@ -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
}
}

Original file line number Diff line number Diff line change
@@ -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;
}
}