@@ -617,9 +617,9 @@ module ts {
617
617
function writeTypeToTextWriter ( type : Type , enclosingDeclaration : Node , flags : TypeFormatFlags , writer : TextWriter ) {
618
618
// TODO(shkamat): usage of enclosingDeclaration
619
619
var typeStack : Type [ ] ;
620
- return writeType ( type ) ;
620
+ return writeType ( type , /*allowFunctionOrConstructorTypeLiteral*/ true ) ;
621
621
622
- function writeType ( type : Type ) {
622
+ function writeType ( type : Type , allowFunctionOrConstructorTypeLiteral : boolean ) {
623
623
if ( type . flags & TypeFlags . Intrinsic ) {
624
624
writer . write ( ( < IntrinsicType > type ) . intrinsicName ) ;
625
625
}
@@ -630,7 +630,7 @@ module ts {
630
630
writer . write ( symbolToString ( type . symbol ) ) ;
631
631
}
632
632
else if ( type . flags & TypeFlags . Anonymous ) {
633
- writeAnonymousType ( < ObjectType > type ) ;
633
+ writeAnonymousType ( < ObjectType > type , allowFunctionOrConstructorTypeLiteral ) ;
634
634
}
635
635
else if ( type . flags & TypeFlags . StringLiteral ) {
636
636
writer . write ( ( < StringLiteralType > type ) . text ) ;
@@ -643,7 +643,9 @@ module ts {
643
643
644
644
function writeTypeReference ( type : TypeReference ) {
645
645
if ( type . target === globalArrayType && ! ( flags & TypeFormatFlags . WriteArrayAsGenericType ) ) {
646
- writeType ( type . typeArguments [ 0 ] ) ;
646
+ // If we are writing array element type the arrow style signatures are not allowed as
647
+ // we need to surround it by curlies, eg. { (): T; }[]; as () => T[] would mean something different
648
+ writeType ( type . typeArguments [ 0 ] , /*allowFunctionOrConstructorTypeLiteral*/ false ) ;
647
649
writer . write ( "[]" ) ;
648
650
}
649
651
else {
@@ -653,18 +655,19 @@ module ts {
653
655
if ( i > 0 ) {
654
656
writer . write ( ", " ) ;
655
657
}
656
- writeType ( type . typeArguments [ i ] ) ;
658
+ writeType ( type . typeArguments [ i ] , /*allowFunctionOrConstructorTypeLiteral*/ true ) ;
657
659
}
658
660
writer . write ( ">" ) ;
659
661
}
660
662
}
661
663
662
- function writeAnonymousType ( type : ObjectType ) {
664
+ function writeAnonymousType ( type : ObjectType , allowFunctionOrConstructorTypeLiteral : boolean ) {
663
665
// Always use 'typeof T' for type of class, enum, and module objects
664
666
if ( type . symbol && type . symbol . flags & ( SymbolFlags . Class | SymbolFlags . Enum | SymbolFlags . ValueModule ) ) {
665
667
writeTypeofSymbol ( type ) ;
666
668
}
667
669
// Use 'typeof T' for types of functions and methods that circularly reference themselves
670
+ // TODO(shkamat): correct the usuage of typeof function - always on functions that are visible
668
671
else if ( type . symbol && type . symbol . flags & ( SymbolFlags . Function | SymbolFlags . Method ) && typeStack && contains ( typeStack , type ) ) {
669
672
writeTypeofSymbol ( type ) ;
670
673
}
@@ -673,7 +676,7 @@ module ts {
673
676
typeStack = [ ] ;
674
677
}
675
678
typeStack . push ( type ) ;
676
- writeLiteralType ( type ) ;
679
+ writeLiteralType ( type , allowFunctionOrConstructorTypeLiteral ) ;
677
680
typeStack . pop ( ) ;
678
681
}
679
682
}
@@ -683,21 +686,24 @@ module ts {
683
686
writer . write ( symbolToString ( type . symbol ) ) ;
684
687
}
685
688
686
- function writeLiteralType ( type : ObjectType ) {
689
+ function writeLiteralType ( type : ObjectType , allowFunctionOrConstructorTypeLiteral : boolean ) {
687
690
var resolved = resolveObjectTypeMembers ( type ) ;
688
691
if ( ! resolved . properties . length && ! resolved . stringIndexType && ! resolved . numberIndexType ) {
689
692
if ( ! resolved . callSignatures . length && ! resolved . constructSignatures . length ) {
690
693
writer . write ( "{}" ) ;
691
694
return ;
692
695
}
693
- if ( resolved . callSignatures . length === 1 && ! resolved . constructSignatures . length ) {
694
- writeSignature ( resolved . callSignatures [ 0 ] , /*arrowStyle*/ true ) ;
695
- return ;
696
- }
697
- if ( resolved . constructSignatures . length === 1 && ! resolved . callSignatures . length ) {
698
- writer . write ( "new " ) ;
699
- writeSignature ( resolved . constructSignatures [ 0 ] , /*arrowStyle*/ true ) ;
700
- return ;
696
+
697
+ if ( allowFunctionOrConstructorTypeLiteral ) {
698
+ if ( resolved . callSignatures . length === 1 && ! resolved . constructSignatures . length ) {
699
+ writeSignature ( resolved . callSignatures [ 0 ] , /*arrowStyle*/ true ) ;
700
+ return ;
701
+ }
702
+ if ( resolved . constructSignatures . length === 1 && ! resolved . callSignatures . length ) {
703
+ writer . write ( "new " ) ;
704
+ writeSignature ( resolved . constructSignatures [ 0 ] , /*arrowStyle*/ true ) ;
705
+ return ;
706
+ }
701
707
}
702
708
}
703
709
@@ -717,13 +723,13 @@ module ts {
717
723
}
718
724
if ( resolved . stringIndexType ) {
719
725
writer . write ( "[x: string]: " ) ;
720
- writeType ( resolved . stringIndexType ) ;
726
+ writeType ( resolved . stringIndexType , /*allowFunctionOrConstructorTypeLiteral*/ true ) ;
721
727
writer . write ( ";" ) ;
722
728
writer . writeLine ( ) ;
723
729
}
724
730
if ( resolved . numberIndexType ) {
725
731
writer . write ( "[x: number]: " ) ;
726
- writeType ( resolved . numberIndexType ) ;
732
+ writeType ( resolved . numberIndexType , /*allowFunctionOrConstructorTypeLiteral*/ true ) ;
727
733
writer . write ( ";" ) ;
728
734
writer . writeLine ( ) ;
729
735
}
@@ -748,7 +754,7 @@ module ts {
748
754
writer . write ( "?" ) ;
749
755
}
750
756
writer . write ( ": " ) ;
751
- writeType ( t ) ;
757
+ writeType ( t , /*allowFunctionOrConstructorTypeLiteral*/ true ) ;
752
758
writer . write ( ";" ) ;
753
759
writer . writeLine ( ) ;
754
760
}
@@ -769,7 +775,7 @@ module ts {
769
775
var constraint = getConstraintOfTypeParameter ( tp ) ;
770
776
if ( constraint ) {
771
777
writer . write ( " extends " ) ;
772
- writeType ( constraint ) ;
778
+ writeType ( constraint , /*allowFunctionOrConstructorTypeLiteral*/ true ) ;
773
779
}
774
780
}
775
781
writer . write ( ">" ) ;
@@ -788,10 +794,10 @@ module ts {
788
794
writer . write ( "?" ) ;
789
795
}
790
796
writer . write ( ": " ) ;
791
- writeType ( getTypeOfSymbol ( p ) ) ;
797
+ writeType ( getTypeOfSymbol ( p ) , /*allowFunctionOrConstructorTypeLiteral*/ true ) ;
792
798
}
793
799
writer . write ( arrowStyle ? ") => " : "): " ) ;
794
- writeType ( getReturnTypeOfSignature ( signature ) ) ;
800
+ writeType ( getReturnTypeOfSignature ( signature ) , /*allowFunctionOrConstructorTypeLiteral*/ true ) ;
795
801
}
796
802
}
797
803
0 commit comments