Skip to content
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

WIP: Polymorphic "this" for static members #29484

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 16 additions & 5 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5588,8 +5588,8 @@ namespace ts {
return type;
}

function getBaseTypeVariableOfClass(symbol: Symbol) {
const baseConstructorType = getBaseConstructorTypeOfClass(getDeclaredTypeOfClassOrInterface(symbol));
function getBaseTypeVariableOfClass(symbol: Symbol, staticType?: Type) {
const baseConstructorType = getBaseConstructorTypeOfClass(getDeclaredTypeOfClassOrInterface(symbol), staticType);
return baseConstructorType.flags & TypeFlags.TypeVariable ? baseConstructorType : undefined;
}

Expand Down Expand Up @@ -5644,7 +5644,7 @@ namespace ts {
}
const type = createObjectType(ObjectFlags.Anonymous, symbol);
if (symbol.flags & SymbolFlags.Class) {
const baseTypeVariable = getBaseTypeVariableOfClass(symbol);
const baseTypeVariable = getBaseTypeVariableOfClass(symbol, type);
return baseTypeVariable ? getIntersectionType([type, baseTypeVariable]) : type;
}
else {
Expand Down Expand Up @@ -5883,7 +5883,7 @@ namespace ts {
* * anyType if the extends expression has type any, or
* * an object type with at least one construct signature.
*/
function getBaseConstructorTypeOfClass(type: InterfaceType): Type {
function getBaseConstructorTypeOfClass(type: InterfaceType, staticType?: Type): Type {
if (!type.resolvedBaseConstructorType) {
const decl = <ClassLikeDeclaration>type.symbol.valueDeclaration;
const extended = getEffectiveBaseTypeNode(decl);
Expand All @@ -5894,11 +5894,22 @@ namespace ts {
if (!pushTypeResolution(type, TypeSystemPropertyName.ResolvedBaseConstructorType)) {
return errorType;
}
const baseConstructorType = checkExpression(baseTypeNode.expression);
let baseConstructorType = checkExpression(baseTypeNode.expression);
if (extended && baseTypeNode !== extended) {
Debug.assert(!extended.typeArguments); // Because this is in a JS file, and baseTypeNode is in an @extends tag
checkExpression(extended.expression);
}

// An interface in this position defines the static members and
// construct signature of base type. In this case, using the
// this type in the interface definition refers to the static
// type of the derivative class.
if (baseConstructorType.symbol && staticType &&
(baseConstructorType.symbol.flags & SymbolFlags.Interface) &&
!isThislessInterface(baseConstructorType.symbol)) {
baseConstructorType = getTypeWithThisArgument(baseConstructorType, staticType);
}

if (baseConstructorType.flags & (TypeFlags.Object | TypeFlags.Intersection)) {
// Resolving the members of a class requires us to resolve the base class of that class.
// We force resolution here such that we catch circularities now.
Expand Down