Skip to content

Commit 6a68b37

Browse files
committed
Do not emit arrow style call signatures when writing T of T[]
1 parent 574c075 commit 6a68b37

File tree

3 files changed

+34
-24
lines changed

3 files changed

+34
-24
lines changed

src/compiler/checker.ts

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -617,9 +617,9 @@ module ts {
617617
function writeTypeToTextWriter(type: Type, enclosingDeclaration: Node, flags: TypeFormatFlags, writer: TextWriter) {
618618
// TODO(shkamat): usage of enclosingDeclaration
619619
var typeStack: Type[];
620-
return writeType(type);
620+
return writeType(type, /*allowFunctionOrConstructorTypeLiteral*/ true);
621621

622-
function writeType(type: Type) {
622+
function writeType(type: Type, allowFunctionOrConstructorTypeLiteral: boolean) {
623623
if (type.flags & TypeFlags.Intrinsic) {
624624
writer.write((<IntrinsicType>type).intrinsicName);
625625
}
@@ -630,7 +630,7 @@ module ts {
630630
writer.write(symbolToString(type.symbol));
631631
}
632632
else if (type.flags & TypeFlags.Anonymous) {
633-
writeAnonymousType(<ObjectType>type);
633+
writeAnonymousType(<ObjectType>type, allowFunctionOrConstructorTypeLiteral);
634634
}
635635
else if (type.flags & TypeFlags.StringLiteral) {
636636
writer.write((<StringLiteralType>type).text);
@@ -643,7 +643,9 @@ module ts {
643643

644644
function writeTypeReference(type: TypeReference) {
645645
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);
647649
writer.write("[]");
648650
}
649651
else {
@@ -653,18 +655,19 @@ module ts {
653655
if (i > 0) {
654656
writer.write(", ");
655657
}
656-
writeType(type.typeArguments[i]);
658+
writeType(type.typeArguments[i], /*allowFunctionOrConstructorTypeLiteral*/ true);
657659
}
658660
writer.write(">");
659661
}
660662
}
661663

662-
function writeAnonymousType(type: ObjectType) {
664+
function writeAnonymousType(type: ObjectType, allowFunctionOrConstructorTypeLiteral: boolean) {
663665
// Always use 'typeof T' for type of class, enum, and module objects
664666
if (type.symbol && type.symbol.flags & (SymbolFlags.Class | SymbolFlags.Enum | SymbolFlags.ValueModule)) {
665667
writeTypeofSymbol(type);
666668
}
667669
// 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
668671
else if (type.symbol && type.symbol.flags & (SymbolFlags.Function | SymbolFlags.Method) && typeStack && contains(typeStack, type)) {
669672
writeTypeofSymbol(type);
670673
}
@@ -673,7 +676,7 @@ module ts {
673676
typeStack = [];
674677
}
675678
typeStack.push(type);
676-
writeLiteralType(type);
679+
writeLiteralType(type, allowFunctionOrConstructorTypeLiteral);
677680
typeStack.pop();
678681
}
679682
}
@@ -683,21 +686,24 @@ module ts {
683686
writer.write(symbolToString(type.symbol));
684687
}
685688

686-
function writeLiteralType(type: ObjectType) {
689+
function writeLiteralType(type: ObjectType, allowFunctionOrConstructorTypeLiteral: boolean) {
687690
var resolved = resolveObjectTypeMembers(type);
688691
if (!resolved.properties.length && !resolved.stringIndexType && !resolved.numberIndexType) {
689692
if (!resolved.callSignatures.length && !resolved.constructSignatures.length) {
690693
writer.write("{}");
691694
return;
692695
}
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+
}
701707
}
702708
}
703709

@@ -717,13 +723,13 @@ module ts {
717723
}
718724
if (resolved.stringIndexType) {
719725
writer.write("[x: string]: ");
720-
writeType(resolved.stringIndexType);
726+
writeType(resolved.stringIndexType, /*allowFunctionOrConstructorTypeLiteral*/ true);
721727
writer.write(";");
722728
writer.writeLine();
723729
}
724730
if (resolved.numberIndexType) {
725731
writer.write("[x: number]: ");
726-
writeType(resolved.numberIndexType);
732+
writeType(resolved.numberIndexType, /*allowFunctionOrConstructorTypeLiteral*/ true);
727733
writer.write(";");
728734
writer.writeLine();
729735
}
@@ -748,7 +754,7 @@ module ts {
748754
writer.write("?");
749755
}
750756
writer.write(": ");
751-
writeType(t);
757+
writeType(t, /*allowFunctionOrConstructorTypeLiteral*/ true);
752758
writer.write(";");
753759
writer.writeLine();
754760
}
@@ -769,7 +775,7 @@ module ts {
769775
var constraint = getConstraintOfTypeParameter(tp);
770776
if (constraint) {
771777
writer.write(" extends ");
772-
writeType(constraint);
778+
writeType(constraint, /*allowFunctionOrConstructorTypeLiteral*/ true);
773779
}
774780
}
775781
writer.write(">");
@@ -788,10 +794,10 @@ module ts {
788794
writer.write("?");
789795
}
790796
writer.write(": ");
791-
writeType(getTypeOfSymbol(p));
797+
writeType(getTypeOfSymbol(p), /*allowFunctionOrConstructorTypeLiteral*/ true);
792798
}
793799
writer.write(arrowStyle ? ") => " : "): ");
794-
writeType(getReturnTypeOfSignature(signature));
800+
writeType(getReturnTypeOfSignature(signature), /*allowFunctionOrConstructorTypeLiteral*/ true);
795801
}
796802
}
797803

tests/baselines/reference/functionDeclarationWithArgumentOfTypeFunctionTypeArray.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,6 @@ function foo(args) {
1111

1212

1313
//// [functionDeclarationWithArgumentOfTypeFunctionTypeArray.d.ts]
14-
declare function foo(args: (x: any) => number[]): number;
14+
declare function foo(args: {
15+
(x: any): number;
16+
}[]): number;

tests/baselines/reference/vardecl.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,9 @@ declare var d2: {
213213
};
214214
};
215215
declare var n2: () => void;
216-
declare var n4: () => void[];
216+
declare var n4: {
217+
(): void;
218+
}[];
217219
declare var d4: {
218220
foo(n: string, x: {
219221
x: number;

0 commit comments

Comments
 (0)