@@ -3549,6 +3549,10 @@ module ts {
3549
3549
return undefined ;
3550
3550
}
3551
3551
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.
3552
3556
function getContextualTypeForInitializerExpression ( node : Expression ) : Type {
3553
3557
var declaration = < VariableDeclaration > node . parent ;
3554
3558
if ( node === declaration . initializer ) {
@@ -3580,6 +3584,7 @@ module ts {
3580
3584
return undefined ;
3581
3585
}
3582
3586
3587
+ // In a typed function call, an argument expression is contextually typed by the type of the corresponding parameter.
3583
3588
function getContextualTypeForArgument ( node : Expression ) : Type {
3584
3589
var callExpression = < CallExpression > node . parent ;
3585
3590
var argIndex = indexOf ( callExpression . arguments , node ) ;
@@ -3594,11 +3599,14 @@ module ts {
3594
3599
var binaryExpression = < BinaryExpression > node . parent ;
3595
3600
var operator = binaryExpression . operator ;
3596
3601
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.
3597
3603
if ( node === binaryExpression . right ) {
3598
3604
return checkExpression ( binaryExpression . left ) ;
3599
3605
}
3600
3606
}
3601
3607
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.
3602
3610
var type = getContextualType ( binaryExpression ) ;
3603
3611
if ( ! type && node === binaryExpression . right ) {
3604
3612
type = checkExpression ( binaryExpression . left ) ;
@@ -3608,6 +3616,9 @@ module ts {
3608
3616
return undefined ;
3609
3617
}
3610
3618
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.
3611
3622
function getContextualTypeForPropertyExpression ( node : Expression ) : Type {
3612
3623
var declaration = < PropertyDeclaration > node . parent ;
3613
3624
var objectLiteral = < ObjectLiteral > declaration . parent ;
@@ -3623,29 +3634,33 @@ module ts {
3623
3634
return undefined ;
3624
3635
}
3625
3636
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.
3626
3640
function getContextualTypeForElementExpression ( node : Expression ) : Type {
3627
3641
var arrayLiteral = < ArrayLiteral > node . parent ;
3628
3642
var type = getContextualType ( arrayLiteral ) ;
3629
3643
if ( type ) {
3630
3644
if ( type . flags & TypeFlags . Tuple ) {
3631
3645
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 ) ;
3637
3649
}
3638
3650
}
3639
3651
return getIndexTypeOfType ( type , IndexKind . Number ) ;
3640
3652
}
3641
3653
return undefined ;
3642
3654
}
3643
3655
3656
+ // In a contextually typed conditional expression, the true/false expressions are contextually typed by the same type.
3644
3657
function getContextualTypeForConditionalOperand ( node : Expression ) : Type {
3645
3658
var conditional = < ConditionalExpression > node . parent ;
3646
3659
return node === conditional . whenTrue || node === conditional . whenFalse ? getContextualType ( conditional ) : undefined ;
3647
3660
}
3648
3661
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.
3649
3664
function getContextualType ( node : Expression ) : Type {
3650
3665
if ( node . contextualType ) {
3651
3666
return node . contextualType ;
@@ -3780,6 +3795,8 @@ module ts {
3780
3795
}
3781
3796
}
3782
3797
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.
3783
3800
function getDeclarationKindFromSymbol ( s : Symbol ) {
3784
3801
return s . valueDeclaration ? s . valueDeclaration . kind : SyntaxKind . Property ;
3785
3802
}
0 commit comments