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

Infer type of method parameter from base class #42953

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
79 changes: 77 additions & 2 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15989,6 +15989,11 @@ namespace ts {
isContextSensitiveFunctionLikeDeclaration(func);
}

function isContextSensitiveClassMethod(func: Node): func is MethodDeclaration {
return noImplicitAny && isClassMethodInSubClass(func) && !(func.type && isTypePredicateNode(func.type)) &&
!getThisParameter(func) && !func.typeParameters && hasContextSensitiveParameters(func);
}

function getTypeWithoutSignatures(type: Type): Type {
if (type.flags & TypeFlags.Object) {
const resolved = resolveStructuredTypeMembers(<ObjectType>type);
Expand Down Expand Up @@ -24203,7 +24208,7 @@ namespace ts {
// Return contextual type of parameter or undefined if no contextual type is available
function getContextuallyTypedParameterType(parameter: ParameterDeclaration): Type | undefined {
const func = parameter.parent;
if (!isContextSensitiveFunctionOrObjectLiteralMethod(func)) {
if (!isContextSensitiveFunctionOrObjectLiteralMethod(func) && !isContextSensitiveClassMethod(func)) {
return undefined;
}
const iife = getImmediatelyInvokedFunctionExpression(func);
Expand Down Expand Up @@ -24581,6 +24586,75 @@ namespace ts {
return getContextualTypeForObjectLiteralElement(node, contextFlags);
}

function getContextualTypeForClassMethod(node: MethodDeclaration): Type | undefined {
if (!isPropertyNameLiteral(node.name) || !isClassMethodInSubClass(node) || !hasContextSensitiveParameters(node)) {
return undefined;
}

const container = cast(node.parent, isClassLike);
const baseTypeNode = getEffectiveBaseTypeNode(container);
Debug.assertIsDefined(baseTypeNode);

const methodDeclType = getTypeOfNode(node);
if (methodDeclType === errorType) {
return undefined;
}

const signature = getSingleCallSignature(methodDeclType);
if (!signature || signature.typeParameters || signature.flags & SignatureFlags.HasRestParameter) {
return undefined;
}

const baseType = getTypeOfNode(baseTypeNode);
if (baseType === errorType) {
return undefined;
}
const baseMethodType = getTypeOfPropertyOfType(baseType, getTextOfPropertyName(node.name));
if (!baseMethodType) {
return undefined;
}

const baseSignature = getSingleCallSignature(baseMethodType);
if (!baseSignature || baseSignature.typeParameters || baseSignature.flags & SignatureFlags.HasRestParameter) {
return undefined;
}

const contextualSignature = resolveContextualClassMethodParameters(baseSignature, signature);
return createAnonymousType(
/*symbol*/undefined,
emptySymbols,
[contextualSignature],
emptyArray,
/*stringIndexInfo*/undefined,
/*numberIndexInfo*/undefined
);
}

function resolveContextualClassMethodParameters(baseSignature: Signature, signature: Signature) {
const newSignatureParameters: Symbol[] = [];
for (let i = 0; i < signature.parameters.length; ++i) {
const parameter = signature.parameters[i];
const parameterDeclaration = isParameter(parameter.valueDeclaration) && parameter.valueDeclaration;
if (!parameterDeclaration || parameterDeclaration.initializer || i >= baseSignature.parameters.length || getEffectiveTypeAnnotationNode(parameterDeclaration)) {
newSignatureParameters.push(parameter);
continue;
}

newSignatureParameters.push(baseSignature.parameters[i]);
}

return createSignature(
signature.declaration,
/*typeParameters*/ undefined,
/*thisParameter*/ undefined,
newSignatureParameters,
signature.resolvedReturnType,
/*resolvedTypePredicate*/ undefined,
signature.minArgumentCount,
SignatureFlags.None
);
}

function getContextualTypeForObjectLiteralElement(element: ObjectLiteralElementLike, contextFlags?: ContextFlags) {
const objectLiteral = <ObjectLiteralExpression>element.parent;
const type = getApparentTypeOfContextualType(objectLiteral, contextFlags);
Expand Down Expand Up @@ -24716,6 +24790,7 @@ namespace ts {
function getApparentTypeOfContextualType(node: Expression | MethodDeclaration, contextFlags?: ContextFlags): Type | undefined {
const contextualType = isObjectLiteralMethod(node) ?
getContextualTypeForObjectLiteralMethod(node, contextFlags) :
isClassMethodInSubClass(node) ? getContextualTypeForClassMethod(node) :
getContextualType(node, contextFlags);
const instantiatedType = instantiateContextualType(contextualType, node, contextFlags);
if (instantiatedType && !(contextFlags && contextFlags & ContextFlags.NoConstraints && instantiatedType.flags & TypeFlags.TypeVariable)) {
Expand Down Expand Up @@ -25142,7 +25217,7 @@ namespace ts {
// all identical ignoring their return type, the result is same signature but with return type as
// union type of return types from these signatures
function getContextualSignature(node: FunctionExpression | ArrowFunction | MethodDeclaration): Signature | undefined {
Debug.assert(node.kind !== SyntaxKind.MethodDeclaration || isObjectLiteralMethod(node));
Debug.assert(node.kind !== SyntaxKind.MethodDeclaration || isObjectLiteralMethod(node) || isClassMethodInSubClass(node));
const typeTagSignature = getSignatureOfTypeTag(node);
if (typeTagSignature) {
return typeTagSignature;
Expand Down
8 changes: 8 additions & 0 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1491,6 +1491,14 @@ namespace ts {
return node && node.kind === SyntaxKind.MethodDeclaration && node.parent.kind === SyntaxKind.ObjectLiteralExpression;
}

export function isClassMethodInSubClass(node: Node): node is MethodDeclaration {
if (!node || !isMethodDeclaration(node) || !isClassLike(node.parent)) {
return false;
}

return !!getClassExtendsHeritageElement(node.parent);
}

export function isObjectLiteralOrClassExpressionMethod(node: Node): node is MethodDeclaration {
return node.kind === SyntaxKind.MethodDeclaration &&
(node.parent.kind === SyntaxKind.ObjectLiteralExpression ||
Expand Down
40 changes: 40 additions & 0 deletions tests/baselines/reference/contextualClassMethodParameter1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//// [contextualClassMethodParameter1.ts]
class Base {
method(x: number) { }
}

class Derived extends Base {
method(x) { }
}


//// [contextualClassMethodParameter1.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 Base = /** @class */ (function () {
function Base() {
}
Base.prototype.method = function (x) { };
return Base;
}());
var Derived = /** @class */ (function (_super) {
__extends(Derived, _super);
function Derived() {
return _super !== null && _super.apply(this, arguments) || this;
}
Derived.prototype.method = function (x) { };
return Derived;
}(Base));
18 changes: 18 additions & 0 deletions tests/baselines/reference/contextualClassMethodParameter1.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
=== tests/cases/conformance/classes/contextualClassMethodParameter/contextualClassMethodParameter1.ts ===
class Base {
>Base : Symbol(Base, Decl(contextualClassMethodParameter1.ts, 0, 0))

method(x: number) { }
>method : Symbol(Base.method, Decl(contextualClassMethodParameter1.ts, 0, 12))
>x : Symbol(x, Decl(contextualClassMethodParameter1.ts, 1, 11))
}

class Derived extends Base {
>Derived : Symbol(Derived, Decl(contextualClassMethodParameter1.ts, 2, 1))
>Base : Symbol(Base, Decl(contextualClassMethodParameter1.ts, 0, 0))

method(x) { }
>method : Symbol(Derived.method, Decl(contextualClassMethodParameter1.ts, 4, 28))
>x : Symbol(x, Decl(contextualClassMethodParameter1.ts, 5, 11))
}

18 changes: 18 additions & 0 deletions tests/baselines/reference/contextualClassMethodParameter1.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
=== tests/cases/conformance/classes/contextualClassMethodParameter/contextualClassMethodParameter1.ts ===
class Base {
>Base : Base

method(x: number) { }
>method : (x: number) => void
>x : number
}

class Derived extends Base {
>Derived : Derived
>Base : Base

method(x) { }
>method : (x: number) => void
>x : number
}

40 changes: 40 additions & 0 deletions tests/baselines/reference/contextualClassMethodParameter10.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//// [contextualClassMethodParameter10.ts]
class Base {
method(x: number) { }
}

class Derived<T> extends Base {
method(x) { }
}


//// [contextualClassMethodParameter10.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 Base = /** @class */ (function () {
function Base() {
}
Base.prototype.method = function (x) { };
return Base;
}());
var Derived = /** @class */ (function (_super) {
__extends(Derived, _super);
function Derived() {
return _super !== null && _super.apply(this, arguments) || this;
}
Derived.prototype.method = function (x) { };
return Derived;
}(Base));
19 changes: 19 additions & 0 deletions tests/baselines/reference/contextualClassMethodParameter10.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
=== tests/cases/conformance/classes/contextualClassMethodParameter/contextualClassMethodParameter10.ts ===
class Base {
>Base : Symbol(Base, Decl(contextualClassMethodParameter10.ts, 0, 0))

method(x: number) { }
>method : Symbol(Base.method, Decl(contextualClassMethodParameter10.ts, 0, 12))
>x : Symbol(x, Decl(contextualClassMethodParameter10.ts, 1, 11))
}

class Derived<T> extends Base {
>Derived : Symbol(Derived, Decl(contextualClassMethodParameter10.ts, 2, 1))
>T : Symbol(T, Decl(contextualClassMethodParameter10.ts, 4, 14))
>Base : Symbol(Base, Decl(contextualClassMethodParameter10.ts, 0, 0))

method(x) { }
>method : Symbol(Derived.method, Decl(contextualClassMethodParameter10.ts, 4, 31))
>x : Symbol(x, Decl(contextualClassMethodParameter10.ts, 5, 11))
}

18 changes: 18 additions & 0 deletions tests/baselines/reference/contextualClassMethodParameter10.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
=== tests/cases/conformance/classes/contextualClassMethodParameter/contextualClassMethodParameter10.ts ===
class Base {
>Base : Base

method(x: number) { }
>method : (x: number) => void
>x : number
}

class Derived<T> extends Base {
>Derived : Derived<T>
>Base : Base

method(x) { }
>method : (x: number) => void
>x : number
}

40 changes: 40 additions & 0 deletions tests/baselines/reference/contextualClassMethodParameter11.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//// [contextualClassMethodParameter11.ts]
class Base<T> {
method(x: T) { }
}

class Derived extends Base<string> {
method(x) { }
}


//// [contextualClassMethodParameter11.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 Base = /** @class */ (function () {
function Base() {
}
Base.prototype.method = function (x) { };
return Base;
}());
var Derived = /** @class */ (function (_super) {
__extends(Derived, _super);
function Derived() {
return _super !== null && _super.apply(this, arguments) || this;
}
Derived.prototype.method = function (x) { };
return Derived;
}(Base));
20 changes: 20 additions & 0 deletions tests/baselines/reference/contextualClassMethodParameter11.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
=== tests/cases/conformance/classes/contextualClassMethodParameter/contextualClassMethodParameter11.ts ===
class Base<T> {
>Base : Symbol(Base, Decl(contextualClassMethodParameter11.ts, 0, 0))
>T : Symbol(T, Decl(contextualClassMethodParameter11.ts, 0, 11))

method(x: T) { }
>method : Symbol(Base.method, Decl(contextualClassMethodParameter11.ts, 0, 15))
>x : Symbol(x, Decl(contextualClassMethodParameter11.ts, 1, 11))
>T : Symbol(T, Decl(contextualClassMethodParameter11.ts, 0, 11))
}

class Derived extends Base<string> {
>Derived : Symbol(Derived, Decl(contextualClassMethodParameter11.ts, 2, 1))
>Base : Symbol(Base, Decl(contextualClassMethodParameter11.ts, 0, 0))

method(x) { }
>method : Symbol(Derived.method, Decl(contextualClassMethodParameter11.ts, 4, 36))
>x : Symbol(x, Decl(contextualClassMethodParameter11.ts, 5, 11))
}

18 changes: 18 additions & 0 deletions tests/baselines/reference/contextualClassMethodParameter11.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
=== tests/cases/conformance/classes/contextualClassMethodParameter/contextualClassMethodParameter11.ts ===
class Base<T> {
>Base : Base<T>

method(x: T) { }
>method : (x: T) => void
>x : T
}

class Derived extends Base<string> {
>Derived : Derived
>Base : Base<string>

method(x) { }
>method : (x: string) => void
>x : string
}

Loading