diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index 2465c336345ae..cd2f51b50aaa4 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -2338,18 +2338,13 @@ namespace ts { if (isPropertyNameLiteral(name) && isPropertyNameLiteral(initializer)) { return getTextOfIdentifierOrLiteral(name) === getTextOfIdentifierOrLiteral(initializer); } - if (isIdentifier(name) && isLiteralLikeAccess(initializer) && + if (isMemberName(name) && isLiteralLikeAccess(initializer) && (initializer.expression.kind === SyntaxKind.ThisKeyword || isIdentifier(initializer.expression) && (initializer.expression.escapedText === "window" || initializer.expression.escapedText === "self" || initializer.expression.escapedText === "global"))) { - - const nameOrArgument = getNameOrArgument(initializer); - if (isPrivateIdentifier(nameOrArgument)) { - Debug.fail("Unexpected PrivateIdentifier in name expression with literal-like access."); - } - return isSameEntityName(name, nameOrArgument); + return isSameEntityName(name, getNameOrArgument(initializer)); } if (isLiteralLikeAccess(name) && isLiteralLikeAccess(initializer)) { return getElementOrPropertyAccessName(name) === getElementOrPropertyAccessName(initializer) diff --git a/tests/baselines/reference/typeFromPrivatePropertyAssignment.js b/tests/baselines/reference/typeFromPrivatePropertyAssignment.js new file mode 100644 index 0000000000000..a762d1b033645 --- /dev/null +++ b/tests/baselines/reference/typeFromPrivatePropertyAssignment.js @@ -0,0 +1,23 @@ +//// [typeFromPrivatePropertyAssignment.ts] +type Foo = { foo?: string }; + +class C { + #a?: Foo; + #b?: Foo; + + m() { + const a = this.#a || {}; + this.#b = this.#b || {}; + } +} + + +//// [typeFromPrivatePropertyAssignment.js] +class C { + #a; + #b; + m() { + const a = this.#a || {}; + this.#b = this.#b || {}; + } +} diff --git a/tests/baselines/reference/typeFromPrivatePropertyAssignment.symbols b/tests/baselines/reference/typeFromPrivatePropertyAssignment.symbols new file mode 100644 index 0000000000000..6f40c1ed256e2 --- /dev/null +++ b/tests/baselines/reference/typeFromPrivatePropertyAssignment.symbols @@ -0,0 +1,32 @@ +=== tests/cases/conformance/classes/members/privateNames/typeFromPrivatePropertyAssignment.ts === +type Foo = { foo?: string }; +>Foo : Symbol(Foo, Decl(typeFromPrivatePropertyAssignment.ts, 0, 0)) +>foo : Symbol(foo, Decl(typeFromPrivatePropertyAssignment.ts, 0, 12)) + +class C { +>C : Symbol(C, Decl(typeFromPrivatePropertyAssignment.ts, 0, 28)) + + #a?: Foo; +>#a : Symbol(C.#a, Decl(typeFromPrivatePropertyAssignment.ts, 2, 9)) +>Foo : Symbol(Foo, Decl(typeFromPrivatePropertyAssignment.ts, 0, 0)) + + #b?: Foo; +>#b : Symbol(C.#b, Decl(typeFromPrivatePropertyAssignment.ts, 3, 13)) +>Foo : Symbol(Foo, Decl(typeFromPrivatePropertyAssignment.ts, 0, 0)) + + m() { +>m : Symbol(C.m, Decl(typeFromPrivatePropertyAssignment.ts, 4, 13)) + + const a = this.#a || {}; +>a : Symbol(a, Decl(typeFromPrivatePropertyAssignment.ts, 7, 13)) +>this.#a : Symbol(C.#a, Decl(typeFromPrivatePropertyAssignment.ts, 2, 9)) +>this : Symbol(C, Decl(typeFromPrivatePropertyAssignment.ts, 0, 28)) + + this.#b = this.#b || {}; +>this.#b : Symbol(C.#b, Decl(typeFromPrivatePropertyAssignment.ts, 3, 13)) +>this : Symbol(C, Decl(typeFromPrivatePropertyAssignment.ts, 0, 28)) +>this.#b : Symbol(C.#b, Decl(typeFromPrivatePropertyAssignment.ts, 3, 13)) +>this : Symbol(C, Decl(typeFromPrivatePropertyAssignment.ts, 0, 28)) + } +} + diff --git a/tests/baselines/reference/typeFromPrivatePropertyAssignment.types b/tests/baselines/reference/typeFromPrivatePropertyAssignment.types new file mode 100644 index 0000000000000..46a7d96539dd3 --- /dev/null +++ b/tests/baselines/reference/typeFromPrivatePropertyAssignment.types @@ -0,0 +1,35 @@ +=== tests/cases/conformance/classes/members/privateNames/typeFromPrivatePropertyAssignment.ts === +type Foo = { foo?: string }; +>Foo : Foo +>foo : string + +class C { +>C : C + + #a?: Foo; +>#a : Foo + + #b?: Foo; +>#b : Foo + + m() { +>m : () => void + + const a = this.#a || {}; +>a : Foo +>this.#a || {} : Foo +>this.#a : Foo +>this : this +>{} : {} + + this.#b = this.#b || {}; +>this.#b = this.#b || {} : Foo +>this.#b : Foo +>this : this +>this.#b || {} : Foo +>this.#b : Foo +>this : this +>{} : {} + } +} + diff --git a/tests/baselines/reference/typeFromPrivatePropertyAssignmentJs.js b/tests/baselines/reference/typeFromPrivatePropertyAssignmentJs.js new file mode 100644 index 0000000000000..439c207a52027 --- /dev/null +++ b/tests/baselines/reference/typeFromPrivatePropertyAssignmentJs.js @@ -0,0 +1,29 @@ +//// [tests/cases/conformance/classes/members/privateNames/typeFromPrivatePropertyAssignmentJs.ts] //// + +//// [typeFromPrivatePropertyAssignmentJs.js] + +//// [a.js] +class C { + /** @type {{ foo?: string } | undefined } */ + #a; + /** @type {{ foo?: string } | undefined } */ + #b; + m() { + const a = this.#a || {}; + this.#b = this.#b || {}; + } +} + + +//// [typeFromPrivatePropertyAssignmentJs.js] +//// [a.js] +class C { + /** @type {{ foo?: string } | undefined } */ + #a; + /** @type {{ foo?: string } | undefined } */ + #b; + m() { + const a = this.#a || {}; + this.#b = this.#b || {}; + } +} diff --git a/tests/baselines/reference/typeFromPrivatePropertyAssignmentJs.symbols b/tests/baselines/reference/typeFromPrivatePropertyAssignmentJs.symbols new file mode 100644 index 0000000000000..83e1ac5d9e33b --- /dev/null +++ b/tests/baselines/reference/typeFromPrivatePropertyAssignmentJs.symbols @@ -0,0 +1,30 @@ +=== tests/cases/conformance/classes/members/privateNames/typeFromPrivatePropertyAssignmentJs.js === + +No type information for this code.=== tests/cases/conformance/classes/members/privateNames/a.js === +class C { +>C : Symbol(C, Decl(a.js, 0, 0)) + + /** @type {{ foo?: string } | undefined } */ + #a; +>#a : Symbol(C.#a, Decl(a.js, 0, 9)) + + /** @type {{ foo?: string } | undefined } */ + #b; +>#b : Symbol(C.#b, Decl(a.js, 2, 7)) + + m() { +>m : Symbol(C.m, Decl(a.js, 4, 7)) + + const a = this.#a || {}; +>a : Symbol(a, Decl(a.js, 6, 13)) +>this.#a : Symbol(C.#a, Decl(a.js, 0, 9)) +>this : Symbol(C, Decl(a.js, 0, 0)) + + this.#b = this.#b || {}; +>this.#b : Symbol(C.#b, Decl(a.js, 2, 7)) +>this : Symbol(C, Decl(a.js, 0, 0)) +>this.#b : Symbol(C.#b, Decl(a.js, 2, 7)) +>this : Symbol(C, Decl(a.js, 0, 0)) + } +} + diff --git a/tests/baselines/reference/typeFromPrivatePropertyAssignmentJs.types b/tests/baselines/reference/typeFromPrivatePropertyAssignmentJs.types new file mode 100644 index 0000000000000..8f92e886cc451 --- /dev/null +++ b/tests/baselines/reference/typeFromPrivatePropertyAssignmentJs.types @@ -0,0 +1,35 @@ +=== tests/cases/conformance/classes/members/privateNames/typeFromPrivatePropertyAssignmentJs.js === + +No type information for this code.=== tests/cases/conformance/classes/members/privateNames/a.js === +class C { +>C : C + + /** @type {{ foo?: string } | undefined } */ + #a; +>#a : { foo?: string; } + + /** @type {{ foo?: string } | undefined } */ + #b; +>#b : { foo?: string; } + + m() { +>m : () => void + + const a = this.#a || {}; +>a : { foo?: string; } +>this.#a || {} : { foo?: string; } +>this.#a : { foo?: string; } +>this : this +>{} : {} + + this.#b = this.#b || {}; +>this.#b = this.#b || {} : { foo?: string; } +>this.#b : { foo?: string; } +>this : this +>this.#b || {} : { foo?: string; } +>this.#b : { foo?: string; } +>this : this +>{} : {} + } +} + diff --git a/tests/cases/conformance/classes/members/privateNames/typeFromPrivatePropertyAssignment.ts b/tests/cases/conformance/classes/members/privateNames/typeFromPrivatePropertyAssignment.ts new file mode 100644 index 0000000000000..02a87b31af7dc --- /dev/null +++ b/tests/cases/conformance/classes/members/privateNames/typeFromPrivatePropertyAssignment.ts @@ -0,0 +1,13 @@ +// @target: esnext + +type Foo = { foo?: string }; + +class C { + #a?: Foo; + #b?: Foo; + + m() { + const a = this.#a || {}; + this.#b = this.#b || {}; + } +} diff --git a/tests/cases/conformance/classes/members/privateNames/typeFromPrivatePropertyAssignmentJs.ts b/tests/cases/conformance/classes/members/privateNames/typeFromPrivatePropertyAssignmentJs.ts new file mode 100644 index 0000000000000..ace038966d076 --- /dev/null +++ b/tests/cases/conformance/classes/members/privateNames/typeFromPrivatePropertyAssignmentJs.ts @@ -0,0 +1,17 @@ +// @target: esnext +// @allowJs: true +// @checkJs: true +// @outDir: ./out +// @filename: typeFromPrivatePropertyAssignmentJs.js + +// @filename: a.js +class C { + /** @type {{ foo?: string } | undefined } */ + #a; + /** @type {{ foo?: string } | undefined } */ + #b; + m() { + const a = this.#a || {}; + this.#b = this.#b || {}; + } +}