Merge pull request #4862 from Microsoft/contextuallyTypeMethodDeclarations

Contextually type method declaration parameters in object literals
This commit is contained in:
Daniel Rosenwasser
2015-09-18 12:34:40 -07:00
13 changed files with 823 additions and 7 deletions

View File

@@ -6574,8 +6574,8 @@ namespace ts {
// Return contextual type of parameter or undefined if no contextual type is available
function getContextuallyTypedParameterType(parameter: ParameterDeclaration): Type {
if (isFunctionExpressionOrArrowFunction(parameter.parent)) {
let func = <FunctionExpression>parameter.parent;
let func = parameter.parent;
if (isFunctionExpressionOrArrowFunction(func) || isObjectLiteralMethod(func)) {
if (isContextSensitive(func)) {
let contextualSignature = getContextualSignature(func);
if (contextualSignature) {
@@ -6907,7 +6907,7 @@ namespace ts {
}
}
function isFunctionExpressionOrArrowFunction(node: Node): boolean {
function isFunctionExpressionOrArrowFunction(node: Node): node is FunctionExpression {
return node.kind === SyntaxKind.FunctionExpression || node.kind === SyntaxKind.ArrowFunction;
}
@@ -6926,8 +6926,8 @@ namespace ts {
function getContextualSignature(node: FunctionExpression | MethodDeclaration): Signature {
Debug.assert(node.kind !== SyntaxKind.MethodDeclaration || isObjectLiteralMethod(node));
let type = isObjectLiteralMethod(node)
? getContextualTypeForObjectLiteralMethod(<MethodDeclaration>node)
: getContextualType(<FunctionExpression>node);
? getContextualTypeForObjectLiteralMethod(node)
: getContextualType(node);
if (!type) {
return undefined;
}
@@ -13656,7 +13656,7 @@ namespace ts {
forEach(node.decorators, checkFunctionAndClassExpressionBodies);
forEach((<MethodDeclaration>node).parameters, checkFunctionAndClassExpressionBodies);
if (isObjectLiteralMethod(node)) {
checkFunctionExpressionOrObjectLiteralMethodBody(<MethodDeclaration>node);
checkFunctionExpressionOrObjectLiteralMethodBody(node);
}
break;
case SyntaxKind.Constructor:

View File

@@ -660,7 +660,7 @@ namespace ts {
return node && node.kind === SyntaxKind.Block && isFunctionLike(node.parent);
}
export function isObjectLiteralMethod(node: Node) {
export function isObjectLiteralMethod(node: Node): node is MethodDeclaration {
return node && node.kind === SyntaxKind.MethodDeclaration && node.parent.kind === SyntaxKind.ObjectLiteralExpression;
}