Skip to content

fix a bug preventing a subclass property sharing a name with a type parameter from being accessed #56690

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

Closed
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
3 changes: 2 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13062,7 +13062,8 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
const symbolTable = createSymbolTable();
// copy all symbols (except type parameters), including the ones with internal names like `InternalSymbolName.Index`
for (const symbol of members.values()) {
if (!(symbol.flags & SymbolFlags.TypeParameter)) {
// include declared properties that share a name with a type parameter by checking for multiple declarations
if (!(symbol.flags & SymbolFlags.TypeParameter) || symbol.declarations?.some(declaration => declaration.kind !== SyntaxKind.TypeParameter)) {
symbolTable.set(symbol.escapedName, symbol);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//// [tests/cases/compiler/classPropertySharingNameWithTypeParameter.ts] ////

//// [classPropertySharingNameWithTypeParameter.ts]
class Leg {}

class Foo<t> extends Leg {
t = {} as t

// should allow this access since t was declared as a property on Foo
foo = this.t
}

//// [classPropertySharingNameWithTypeParameter.js]
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var Leg = /** @class */ (function () {
function Leg() {
}
return Leg;
}());
var Foo = /** @class */ (function (_super) {
__extends(Foo, _super);
function Foo() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this.t = {};
// should allow this access since t was declared as a property on Foo
_this.foo = _this.t;
return _this;
}
return Foo;
}(Leg));
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//// [tests/cases/compiler/classPropertySharingNameWithTypeParameter.ts] ////

=== classPropertySharingNameWithTypeParameter.ts ===
class Leg {}
>Leg : Symbol(Leg, Decl(classPropertySharingNameWithTypeParameter.ts, 0, 0))

class Foo<t> extends Leg {
>Foo : Symbol(Foo, Decl(classPropertySharingNameWithTypeParameter.ts, 0, 12))
>t : Symbol(t, Decl(classPropertySharingNameWithTypeParameter.ts, 2, 10), Decl(classPropertySharingNameWithTypeParameter.ts, 2, 26))
>Leg : Symbol(Leg, Decl(classPropertySharingNameWithTypeParameter.ts, 0, 0))

t = {} as t
>t : Symbol(t, Decl(classPropertySharingNameWithTypeParameter.ts, 2, 10), Decl(classPropertySharingNameWithTypeParameter.ts, 2, 26))
>t : Symbol(t, Decl(classPropertySharingNameWithTypeParameter.ts, 2, 10), Decl(classPropertySharingNameWithTypeParameter.ts, 2, 26))

// should allow this access since t was declared as a property on Foo
foo = this.t
>foo : Symbol(Foo.foo, Decl(classPropertySharingNameWithTypeParameter.ts, 3, 15))
>this.t : Symbol(t, Decl(classPropertySharingNameWithTypeParameter.ts, 2, 10), Decl(classPropertySharingNameWithTypeParameter.ts, 2, 26))
>this : Symbol(Foo, Decl(classPropertySharingNameWithTypeParameter.ts, 0, 12))
>t : Symbol(t, Decl(classPropertySharingNameWithTypeParameter.ts, 2, 10), Decl(classPropertySharingNameWithTypeParameter.ts, 2, 26))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//// [tests/cases/compiler/classPropertySharingNameWithTypeParameter.ts] ////

=== classPropertySharingNameWithTypeParameter.ts ===
class Leg {}
>Leg : Leg

class Foo<t> extends Leg {
>Foo : Foo<t>
>Leg : Leg

t = {} as t
>t : t
>{} as t : t
>{} : {}

// should allow this access since t was declared as a property on Foo
foo = this.t
>foo : t
>this.t : t
>this : this
>t : t
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class Leg {}

class Foo<t> extends Leg {
t = {} as t

// should allow this access since t was declared as a property on Foo
foo = this.t
}