8000 Truncate long types in error messages · seanhess/TypeScript@9f3d83a · GitHub
[go: up one dir, main page]

Skip to content

Commit 9f3d83a

Browse files
committed
Truncate long types in error messages
1 parent 18c1789 commit 9f3d83a

14 files changed

+46
-32
lines changed

src/compiler/checker.ts

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -943,20 +943,36 @@ module ts {
943943
writer.write(symbolToString(symbol, enclosingDeclaration, meaning));
944944
}
945945

946-
function createSingleLineTextWriter() {
946+
function createSingleLineTextWriter(maxLength?: number) {
947947
var result = "";
948+
var overflow = false;
949+
function write(s: string) {
950+
if (!overflow) {
951+
result += s;
952+
if (result.length > maxLength) {
953+
result = result.substr(0, maxLength - 3) + "...";
954+
overflow = true;
955+
}
956+
}
957+
}
948958
return {
949-
write(s: string) { result += s; },
950-
writeSymbol(symbol: Symbol, enclosingDeclaration?: Node, meaning?: SymbolFlags) { writeSymbolToTextWriter(symbol, enclosingDeclaration, meaning, this); },
951-
writeLine() { result += " "; },
959+
write: write,
960+
writeSymbol(symbol: Symbol, enclosingDeclaration?: Node, meaning?: SymbolFlags) {
961+
writeSymbolToTextWriter(symbol, enclosingDeclaration, meaning, this);
962+
},
963+
writeLine() {
964+
write(" ");
965+
},
952966
increaseIndent() { },
953967
decreaseIndent() { },
954-
getText() { return result; }
968+
getText() {
969+
return result;
970+
}
955971
};
956972
}
957973

958974
function typeToString(type: Type, enclosingDeclaration?: Node, flags?: TypeFormatFlags): string {
959-
var stringWriter = createSingleLineTextWriter();
975+
var stringWriter = createSingleLineTextWriter(flags & TypeFormatFlags.NoTruncation ? undefined : 100);
960976
// TODO(shkamat): typeToString should take enclosingDeclaration as input, once we have implemented enclosingDeclaration
961977
writeTypeToTextWriter(type, enclosingDeclaration, flags, stringWriter);
962978
return stringWriter.getText();

src/compiler/types.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -631,12 +631,10 @@ module ts {
631631
}
632632

633633
export enum TypeFormatFlags {
634-
None = 0x00000000,
635-
636-
/** writes Array<T> instead T[] */
637-
WriteArrayAsGenericType = 0x00000001, // Declarations
638-
639-
UseTypeOfFunction = 0x00000002, // instead of writing signature type of function use typeof
634+
None = 0x00000000,
635+
WriteArrayAsGenericType = 0x00000001, // Write Array<T> instead T[]
636+
UseTypeOfFunction = 0x00000002, // Write typeof instead of function type literal
637+
NoTruncation = 0x00000004, // Don't truncate typeToString result
640638
}
641639

642640
export enum SymbolAccessibility {

src/harness/typeWriter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ class TypeWriterWalker {
8686
column: lineAndCharacter.character,
8787
syntaxKind: ts.SyntaxKind[node.kind],
8888
sourceText: sourceText,
89-
type: this.checker.typeToString(type, node.parent, ts.TypeFormatFlags.None)
89+
type: this.checker.typeToString(type, node.parent, ts.TypeFormatFlags.NoTruncation)
9090
});
9191
}
9292

tests/baselines/reference/arrayTypeOfTypeOf.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@
1010
~
1111
!!! Expression expected.
1212
~~~
13-
!!! Type 'number' is not assignable to type '{ (arrayLength?: number): any[]; <T>(arrayLength: number): T[]; <T>(...items: T[]): T[]; new (arrayLength?: number): any[]; new <T>(arrayLength: number): T[]; new <T>(...items: T[]): T[]; isArray(arg: any): boolean; prototype: any[]; }':
13+
!!! Type 'number' is not assignable to type '{ (arrayLength?: number): any[]; <T>(arrayLength: number): T[]; <T>(...items: T[]): T[]; new (arr...':
1414
!!! Property 'isArray' is missing in type 'Number'.
1515
var xs4: typeof Array<typeof x>;
1616
~
1717
!!! '=' expected.
1818
~
1919
!!! Expression expected.
2020
~~~
21-
!!! Type 'number' is not assignable to type '{ (arrayLength?: number): any[]; <T>(arrayLength: number): T[]; <T>(...items: T[]): T[]; new (arrayLength?: number): any[]; new <T>(arrayLength: number): T[]; new <T>(...items: T[]): T[]; isArray(arg: any): boolean; prototype: any[]; }'.
21+
!!! Type 'number' is not assignable to type '{ (arrayLength?: number): any[]; <T>(arrayLength: number): T[]; <T>(...items: T[]): T[]; new (arr...'.

tests/baselines/reference/assignmentCompatWithCallSignatures4.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
var b8: <T extends Base, U extends Derived>(x: (arg: T) => U, y: (arg2: { foo: number; }) => U) => (r: T) => U;
5353
a8 = b8; // error, { foo: number } and Base are incompatible
5454
~~
55-
!!! Type '<T extends Base, U extends Derived>(x: (arg: T) => U, y: (arg2: { foo: number; }) => U) => (r: T) => U' is not assignable to type '(x: (arg: Base) => Derived, y: (arg2: Base) => Derived) => (r: Base) => Derived':
55+
!!! Type '<T extends Base, U extends Derived>(x: (arg: T) => U, y: (arg2: { foo: number; }) => U) => (r: T)...' is not assignable to type '(x: (arg: Base) => Derived, y: (arg2: Base) => Derived) => (r: Base) => Derived':
5656
!!! Types of parameters 'y' and 'y' are incompatible:
5757
!!! Type '(arg2: { foo: number; }) => any' is not assignable to type '(arg2: Base) => Derived':
5858
!!! Types of parameters 'arg2' and 'arg2' are incompatible:
@@ -61,7 +61,7 @@
6161
!!! Type 'number' is not assignable to type 'string'.
6262
b8 = a8; // error, { foo: number } and Base are incompatible
6363
~~
64-
!!! Type '(x: (arg: Base) => Derived, y: (arg2: Base) => Derived) => (r: Base) => Derived' is not assignable to type '<T extends Base, U extends Derived>(x: (arg: T) => U, y: (arg2: { foo: number; }) => U) => (r: T) => U':
64+
!!! Type '(x: (arg: Base) => Derived, y: (arg2: Base) => Derived) => (r: Base) => Derived' is not assignable to type '<T extends Base, U extends Derived>(x: (arg: T) => U, y: (arg2: { foo: number; }) => U) => (r: T)...':
6565
!!! Types of parameters 'y' and 'y' are incompatible:
6666
!!! Type '(arg2: Base) => Derived' is not assignable to type '(arg2: { foo: number; }) => any':
6767
!!! Types of parameters 'arg2' and 'arg2' are incompatible:

tests/baselines/reference/assignmentCompatWithConstructSignatures4.errors.txt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
var b8: new <T extends Base, U extends Derived>(x: (arg: T) => U, y: (arg2: { foo: number; }) => U) => (r: T) => U;
5353
a8 = b8; // error, type mismatch
5454
~~
55-
!!! Type 'new <T extends Base, U extends Derived>(x: (arg: T) => U, y: (arg2: { foo: number; }) => U) => (r: T) => U' is not assignable to type 'new (x: (arg: Base) => Derived, y: (arg2: Base) => Derived) => (r: Base) => Derived':
55+
!!! Type 'new <T extends Base, U extends Derived>(x: (arg: T) => U, y: (arg2: { foo: number; }) => U) => (r...' is not assignable to type 'new (x: (arg: Base) => Derived, y: (arg2: Base) => Derived) => (r: Base) => Derived':
5656
!!! Types of parameters 'y' and 'y' are incompatible:
5757
!!! Type '(arg2: { foo: number; }) => any' is not assignable to type '(arg2: Base) => Derived':
5858
!!! Types of parameters 'arg2' and 'arg2' are incompatible:
@@ -61,7 +61,7 @@
6161
!!! Type 'number' is not assignable to type 'string'.
6262
b8 = a8; // error
6363
~~
64-
!!! Type 'new (x: (arg: Base) => Derived, y: (arg2: Base) => Derived) => (r: Base) => Derived' is not assignable to type 'new <T extends Base, U extends Derived>(x: (arg: T) => U, y: (arg2: { foo: number; }) => U) => (r: T) => U':
64+
!!! Type 'new (x: (arg: Base) => Derived, y: (arg2: Base) => Derived) => (r: Base) => Derived' is not assignable to type 'new <T extends Base, U extends Derived>(x: (arg: T) => U, y: (arg2: { foo: number; }) => U) => (r...':
6565
!!! Types of parameters 'y' and 'y' are incompatible:
6666
!!! Type '(arg2: Base) => Derived' is not assignable to type '(arg2: { foo: number; }) => any':
6767
!!! Types of parameters 'arg2' and 'arg2' are incompatible:
@@ -93,24 +93,24 @@
9393
var b16: new <T>(x: (a: T) => T) => T[];
9494
a16 = b16; // error
9595
~~~
96-
!!! Type 'new <T>(x: (a: T) => T) => T[]' is not assignable to type '{ new (x: { new (a: number): number; new (a?: number): number; }): number[]; new (x: { new (a: boolean): boolean; new (a?: boolean): boolean; }): boolean[]; }':
96+
!!! Type 'new <T>(x: (a: T) => T) => T[]' is not assignable to type '{ new (x: { new (a: number): number; new (a?: number): number; }): number[]; new (x: { new (a: bo...':
9797
!!! Types of parameters 'x' and 'x' are incompatible:
9898
!!! Type '(a: any) => any' is not assignable to type '{ new (a: number): number; new (a?: number): number; }'.
9999
b16 = a16; // error
100100
~~~
101-
!!! Type '{ new (x: { new (a: number): number; new (a?: number): number; }): number[]; new (x: { new (a: boolean): boolean; new (a?: boolean): boolean; }): boolean[]; }' is not assignable to type 'new <T>(x: (a: T) => T) => T[]':
101+
!!! Type '{ new (x: { new (a: number): number; new (a?: number): number; }): number[]; new (x: { new (a: bo...' is not assignable to type 'new <T>(x: (a: T) => T) => T[]':
102102
!!! Types of parameters 'x' and 'x' are incompatible:
103103
!!! Type '{ new (a: number): number; new (a?: number): number; }' is not assignable to type '(a: any) => any'.
104104

105105
var b17: new <T>(x: (a: T) => T) => any[];
106106
a17 = b17; // error
107107
~~~
108-
!!! Type 'new <T>(x: (a: T) => T) => any[]' is not assignable to type '{ new (x: { new <T extends Derived>(a: T): T; new <T extends Base>(a: T): T; }): any[]; new (x: { new <T extends Derived2>(a: T): T; new <T extends Base>(a: T): T; }): any[]; }':
108+
!!! Type 'new <T>(x: (a: T) => T) => any[]' is not assignable to type '{ new (x: { new <T extends Derived>(a: T): T; new <T extends Base>(a: T): T; }): any[]; new (x: {...':
109109
!!! Types of parameters 'x' and 'x' are incompatible:
110110
!!! Type '(a: any) => any' is not assignable to type '{ new <T extends Derived>(a: T): T; new <T extends Base>(a: T): T; }'.
111111
b17 = a17; // error
112112
~~~
113-
!!! Type '{ new (x: { new <T extends Derived>(a: T): T; new <T extends Base>(a: T): T; }): any[]; new (x: { new <T extends Derived2>(a: T): T; new <T extends Base>(a: T): T; }): any[]; }' is not assignable to type 'new <T>(x: (a: T) => T) => any[]':
113+
!!! Type '{ new (x: { new <T extends Derived>(a: T): T; new <T extends Base>(a: T): T; }): any[]; new (x: {...' is not assignable to type 'new <T>(x: (a: T) => T) => any[]':
114114
!!! Types of parameters 'x' and 'x' are incompatible:
115115
!!! Type '{ new <T extends Derived>(a: T): T; new <T extends Base>(a: T): T; }' is not assignable to type '(a: any) => any'.
116116
}

tests/baselines/reference/callSignatureAssignabilityInInheritance3.errors.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
~~
6969
!!! Interface 'I4' incorrectly extends interface 'A':
7070
!!! Types of property 'a8' are incompatible:
71-
!!! Type '<T extends Base, U extends Derived>(x: (arg: T) => U, y: (arg2: { foo: number; }) => U) => (r: T) => U' is not assignable to type '(x: (arg: Base) => Derived, y: (arg2: Base) => Derived) => (r: Base) => Derived':
71+
!!! Type '<T extends Base, U extends Derived>(x: (arg: T) => U, y: (arg2: { foo: number; }) => U) => (r: T)...' is not assignable to type '(x: (arg: Base) => Derived, y: (arg2: Base) => Derived) => (r: Base) => Derived':
7272
!!! Types of parameters 'y' and 'y' are incompatible:
7373
!!! Type '(arg2: { foo: number; }) => any' is not assignable to type '(arg2: Base) => Derived':
7474
!!! Types of parameters 'arg2' and 'arg2' are incompatible:

tests/baselines/reference/constructSignatureAssignabilityInInheritance3.errors.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
~~
5959
!!! Interface 'I4' incorrectly extends interface 'A':
6060
!!! Types of property 'a8' are incompatible:
61-
!!! Type 'new <T extends Base, U extends Derived>(x: (arg: T) => U, y: (arg2: { foo: number; }) => U) => (r: T) => U' is not assignable to type 'new (x: (arg: Base) => Derived, y: (arg2: Base) => Derived) => (r: Base) => Derived':
61+
!!! Type 'new <T extends Base, U extends Derived>(x: (arg: T) => U, y: (arg2: { foo: number; }) => U) => (r...' is not assignable to type 'new (x: (arg: Base) => Derived, y: (arg2: Base) => Derived) => (r: Base) => Derived':
6262
!!! Types of parameters 'y' and 'y' are incompatible:
6363
!!! Type '(arg2: { foo: number; }) => any' is not assignable to type '(arg2: Base) => Derived':
6464
!!! Types of parameters 'arg2' and 'arg2' are incompatible:

tests/baselines/reference/errorMessageOnObjectLiteralType.errors.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@
88
!!! Property 'getOwnPropertyNamess' does not exist on type '{ a: string; b: number; }'.
99
Object.getOwnPropertyNamess(null);
1010
~~~~~~~~~~~~~~~~~~~~
11-
!!! Property 'getOwnPropertyNamess' does not exist on type '{ (): any; (value: any): any; new (value?: any): Object; prototype: Object; getPrototypeOf(o: any): any; getOwnPropertyDescriptor(o: any, p: string): PropertyDescriptor; getOwnPropertyNames(o: any): string[]; create(o: any, properties?: PropertyDescriptorMap): any; defineProperty(o: any, p: string, attributes: PropertyDescriptor): any; defineProperties(o: any, properties: PropertyDescriptorMap): any; seal(o: any): any; freeze(o: any): any; preventExtensions(o: any): any; isSealed(o: any): boolean; isFrozen(o: any): boolean; isExtensible(o: any): boolean; keys(o: any): string[]; }'.
11+
!!! Property 'getOwnPropertyNamess' does not exist on type '{ (): any; (value: any): any; new (value?: any): Object; prototype: Object; getPrototypeOf(o: any...'.

tests/baselines/reference/numericIndexerConstrainsPropertyDeclarations.errors.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@
9898
// error
9999
var b: { [x: number]: string; } = {
100100
~
101-
!!! Type '{ [x: number]: {}; 1.0: string; 2.0: number; a: string; b: number; c: () => void; "d": string; "e": number; "3.0": string; "4.0": number; f: unknown; X: string; foo: () => string; }' is not assignable to type '{ [x: number]: string; }':
101+
!!! Type '{ [x: number]: {}; 1.0: string; 2.0: number; a: string; b: number; c: () => void; "d": string; "e...' is not assignable to type '{ [x: number]: string; }':
102102
!!! Index signatures are incompatible:
103103
!!! Type '{}' is not assignable to type 'string'.
104104
a: '',

0 commit comments

Comments
 (0)
0