Skip to content

Commit ef52312

Browse files
committed
Addressing CR feedback.
1 parent 3b1dbad commit ef52312

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

src/compiler/checker.ts

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3549,6 +3549,10 @@ module ts {
35493549
return undefined;
35503550
}
35513551

3552+
// In a variable, parameter or property declaration with a type annotation, the contextual type of an initializer
3553+
// expression is the type of the variable, parameter or property. In a parameter declaration of a contextually
3554+
// typed function expression, the contextual type of an initializer expression is the contextual type of the
3555+
// parameter.
35523556
function getContextualTypeForInitializerExpression(node: Expression): Type {
35533557
var declaration = <VariableDeclaration>node.parent;
35543558
if (node === declaration.initializer) {
@@ -3580,6 +3584,7 @@ module ts {
35803584
return undefined;
35813585
}
35823586

3587+
// In a typed function call, an argument expression is contextually typed by the type of the corresponding parameter.
35833588
function getContextualTypeForArgument(node: Expression): Type {
35843589
var callExpression = <CallExpression>node.parent;
35853590
var argIndex = indexOf(callExpression.arguments, node);
@@ -3594,11 +3599,14 @@ module ts {
35943599
var binaryExpression = <BinaryExpression>node.parent;
35953600
var operator = binaryExpression.operator;
35963601
if (operator >= SyntaxKind.FirstAssignment && operator <= SyntaxKind.LastAssignment) {
3602+
// In an assignment expression, the right operand is contextually typed by the type of the left operand.
35973603
if (node === binaryExpression.right) {
35983604
return checkExpression(binaryExpression.left);
35993605
}
36003606
}
36013607
else if (operator === SyntaxKind.BarBarToken) {
3608+
// When an || expression has a contextual type, the operands are contextually typed by that type. When an ||
3609+
// expression has no contextual type, the right operand is contextually typed by the type of the left operand.
36023610
var type = getContextualType(binaryExpression);
36033611
if (!type && node === binaryExpression.right) {
36043612
type = checkExpression(binaryExpression.left);
@@ -3608,6 +3616,9 @@ module ts {
36083616
return undefined;
36093617
}
36103618

3619+
// In an object literal contextually typed by a type T, the contextual type of a property assignment is the type of
3620+
// the matching property in T, if one exists. Otherwise, it is the type of the numeric index signature in T, if one
3621+
// exists. Otherwise, it is the type of the string index signature in T, if one exists.
36113622
function getContextualTypeForPropertyExpression(node: Expression): Type {
36123623
var declaration = <PropertyDeclaration>node.parent;
36133624
var objectLiteral = <ObjectLiteral>declaration.parent;
@@ -3623,29 +3634,33 @@ module ts {
36233634
return undefined;
36243635
}
36253636

3637+
// In an array literal contextually typed by a type T, the contextual type of an element expression is the corresponding
3638+
// tuple element type in T, if one exists and T is a tuple type. Otherwise, it is the type of the numeric index signature
3639+
// in T, if one exists.
36263640
function getContextualTypeForElementExpression(node: Expression): Type {
36273641
var arrayLiteral = <ArrayLiteral>node.parent;
36283642
var type = getContextualType(arrayLiteral);
36293643
if (type) {
36303644
if (type.flags & TypeFlags.Tuple) {
36313645
var index = indexOf(arrayLiteral.elements, node);
3632-
if (index >= 0) {
3633-
var prop = getPropertyOfType(type, "" + index);
3634-
if (prop) {
3635-
return getTypeOfSymbol(prop);
3636-
}
3646+
var prop = getPropertyOfType(type, "" + index);
3647+
if (prop) {
3648+
return getTypeOfSymbol(prop);
36373649
}
36383650
}
36393651
return getIndexTypeOfType(type, IndexKind.Number);
36403652
}
36413653
return undefined;
36423654
}
36433655

3656+
// In a contextually typed conditional expression, the true/false expressions are contextually typed by the same type.
36443657
function getContextualTypeForConditionalOperand(node: Expression): Type {
36453658
var conditional = <ConditionalExpression>node.parent;
36463659
return node === conditional.whenTrue || node === conditional.whenFalse ? getContextualType(conditional) : undefined;
36473660
}
36483661

3662+
// Return the contextual type for a given expression node. During overload resolution, a contextual type may temporarily
3663+
// be "pushed" onto a node using the contextualType property.
36493664
function getContextualType(node: Expression): Type {
36503665
if (node.contextualType) {
36513666
return node.contextualType;
@@ -3780,6 +3795,8 @@ module ts {
37803795
}
37813796
}
37823797

3798+
// If a symbol is a synthesized symbol with no value declaration, we assume it is a property. Example of this are the synthesized
3799+
// '.prototype' property as well as synthesized tuple index properties.
37833800
function getDeclarationKindFromSymbol(s: Symbol) {
37843801
return s.valueDeclaration ? s.valueDeclaration.kind : SyntaxKind.Property;
37853802
}

0 commit comments

Comments
 (0)