@@ -3758,19 +3758,17 @@ module ts {
3758
3758
return undefined ;
3759
3759
}
3760
3760
3761
- // In an array literal contextually typed by a type T, the contextual type of an element expression is the corresponding
3762
- // tuple element type in T, if one exists and T is a tuple type . Otherwise, it is the type of the numeric index signature
3763
- // in T, if one exists.
3761
+ // In an array literal contextually typed by a type T, the contextual type of an element expression at index N is
3762
+ // the type of the property with the numeric name N in T, if one exists . Otherwise, it is the type of the numeric
3763
+ // index signature in T, if one exists.
3764
3764
function getContextualTypeForElementExpression ( node : Expression ) : Type {
3765
3765
var arrayLiteral = < ArrayLiteral > node . parent ;
3766
3766
var type = getContextualType ( arrayLiteral ) ;
3767
3767
if ( type ) {
3768
- if ( type . flags & TypeFlags . Tuple ) {
3769
- var index = indexOf ( arrayLiteral . elements , node ) ;
3770
- var prop = getPropertyOfType ( type , "" + index ) ;
3771
- if ( prop ) {
3772
- return getTypeOfSymbol ( prop ) ;
3773
- }
3768
+ var index = indexOf ( arrayLiteral . elements , node ) ;
3769
+ var prop = getPropertyOfType ( type , "" + index ) ;
3770
+ if ( prop ) {
3771
+ return getTypeOfSymbol ( prop ) ;
3774
3772
}
3775
3773
return getIndexTypeOfType ( type , IndexKind . Number ) ;
3776
3774
}
@@ -3837,21 +3835,24 @@ module ts {
3837
3835
3838
3836
function checkArrayLiteral ( node : ArrayLiteral , contextualMapper ?: TypeMapper ) : Type {
3839
3837
var contextualType = getContextualType ( node ) ;
3840
- var isTupleLiteral = contextualType && ( contextualType . flags & TypeFlags . Tuple ) !== 0 ;
3838
+ var elements = node . elements ;
3841
3839
var elementTypes : Type [ ] = [ ] ;
3842
- forEach ( node . elements , element => {
3843
- var type = element . kind !== SyntaxKind . OmittedExpression ? checkExpression ( element , contextualMapper ) : undefinedType ;
3844
- if ( isTupleLiteral || ! contains ( elementTypes , type ) ) {
3845
- elementTypes . push ( type ) ;
3840
+ var isTupleLiteral : boolean = false ;
3841
+ for ( var i = 0 ; i < elements . length ; i ++ ) {
3842
+ if ( contextualType && getPropertyOfType ( contextualType , "" + i ) ) {
3843
+ isTupleLiteral = true ;
3846
3844
}
3847
- } ) ;
3845
+ var element = elements [ i ] ;
3846
+ var type = element . kind !== SyntaxKind . OmittedExpression ? checkExpression ( element , contextualMapper ) : undefinedType ;
3847
+ elementTypes . push ( type ) ;
3848
+ }
3848
3849
if ( isTupleLiteral ) {
3849
3850
return createTupleType ( elementTypes ) ;
3850
3851
}
3851
3852
var contextualElementType = contextualType && ! isInferentialContext ( contextualMapper ) ? getIndexTypeOfType ( contextualType , IndexKind . Number ) : undefined ;
3852
- var elementType = getBestCommonType ( elementTypes , contextualElementType , true ) ;
3853
+ var elementType = getBestCommonType ( uniqueElements ( elementTypes ) , contextualElementType , true ) ;
3853
3854
if ( ! elementType ) {
3854
- elementType = elementTypes . length ? emptyObjectType : undefinedType ;
3855
+ elementType = elements . length ? emptyObjectType : undefinedType ;
3855
3856
}
3856
3857
return createArrayType ( elementType ) ;
3857
3858
}
0 commit comments