Skip to content

Try get method type from super class #41303

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
wants to merge 4 commits into from
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
33 changes: 28 additions & 5 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15546,8 +15546,8 @@ namespace ts {
return !node.typeParameters && !getEffectiveReturnTypeNode(node) && !!node.body && node.body.kind !== SyntaxKind.Block && isContextSensitive(node.body);
}

function isContextSensitiveFunctionOrObjectLiteralMethod(func: Node): func is FunctionExpression | ArrowFunction | MethodDeclaration {
return (isInJSFile(func) && isFunctionDeclaration(func) || isFunctionExpressionOrArrowFunction(func) || isObjectLiteralMethod(func)) &&
function isContextSensitiveFunctionOrObjectLiteralMethodOrClassMethod(func: Node): func is FunctionExpression | ArrowFunction | MethodDeclaration {
return (isInJSFile(func) && isFunctionDeclaration(func) || isFunctionExpressionOrArrowFunction(func) || isObjectLiteralMethod(func) || (noImplicitAny && isClassMethodInSubClass(func))) &&
isContextSensitiveFunctionLikeDeclaration(func);
}

Expand Down Expand Up @@ -23549,7 +23549,7 @@ namespace ts {
if (func.kind === SyntaxKind.ArrowFunction) {
return undefined;
}
if (isContextSensitiveFunctionOrObjectLiteralMethod(func)) {
if (isContextSensitiveFunctionOrObjectLiteralMethodOrClassMethod(func)) {
const contextualSignature = getContextualSignature(func);
if (contextualSignature) {
const thisParameter = contextualSignature.thisParameter;
Expand Down Expand Up @@ -23609,7 +23609,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 (!isContextSensitiveFunctionOrObjectLiteralMethodOrClassMethod(func)) {
return undefined;
}
const iife = getImmediatelyInvokedFunctionExpression(func);
Expand Down Expand Up @@ -23987,6 +23987,28 @@ namespace ts {
return getContextualTypeForObjectLiteralElement(node, contextFlags);
}

function getContextualTypeForClassMethod(node: MethodDeclaration): Type | undefined {
if (node.type || !isPropertyNameLiteral(node.name) || some(node.parameters, parameter => !!parameter.type)) {
return undefined;
}

const container = cast(node.parent, isClassLike);
const heritageElement = getClassExtendsHeritageElement(container);
Debug.assertIsDefined(heritageElement);

const baseType = getTypeOfNode(heritageElement);
if (baseType === errorType) {
return undefined;
}

const basePropType = getTypeOfPropertyOfType(baseType, getTextOfPropertyName(node.name));
if (!basePropType) {
return undefined;
}

return basePropType;
}

function getContextualTypeForObjectLiteralElement(element: ObjectLiteralElementLike, contextFlags?: ContextFlags) {
const objectLiteral = <ObjectLiteralExpression>element.parent;
const type = getApparentTypeOfContextualType(objectLiteral, contextFlags);
Expand Down Expand Up @@ -24122,6 +24144,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 @@ -24453,7 +24476,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 @@ -1462,6 +1462,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
42 changes: 42 additions & 0 deletions tests/baselines/reference/parameterTypeOfMethodLike.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//// [parameterTypeOfMethodLike.ts]
class Base {
method(x: number) { }
}

class Derived extends Base {
method(x) {
x.toFixed(0);
}
}

//// [parameterTypeOfMethodLike.js]
"use strict";
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) {
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) {
x.toFixed(0);
};
return Derived;
}(Base));
23 changes: 23 additions & 0 deletions tests/baselines/reference/parameterTypeOfMethodLike.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
=== tests/cases/conformance/classes/methodDeclarations/parameterTypeOfMethodLike.ts ===
class Base {
>Base : Symbol(Base, Decl(parameterTypeOfMethodLike.ts, 0, 0))

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

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

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

x.toFixed(0);
>x.toFixed : Symbol(Number.toFixed, Decl(lib.es5.d.ts, --, --))
>x : Symbol(x, Decl(parameterTypeOfMethodLike.ts, 5, 11))
>toFixed : Symbol(Number.toFixed, Decl(lib.es5.d.ts, --, --))
}
}
25 changes: 25 additions & 0 deletions tests/baselines/reference/parameterTypeOfMethodLike.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
=== tests/cases/conformance/classes/methodDeclarations/parameterTypeOfMethodLike.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

x.toFixed(0);
>x.toFixed(0) : string
>x.toFixed : (fractionDigits?: number | undefined) => string
>x : number
>toFixed : (fractionDigits?: number | undefined) => string
>0 : 0
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// @strict: true

class Base {
method(x: number) { }
}

class Derived extends Base {
method(x) {
x.toFixed(0);
}
}