Skip to content

Type width in error messages #1099

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion internal/checker/relater.go
Original file line number Diff line number Diff line change
Expand Up @@ -4705,7 +4705,14 @@ func (r *Relater) reportRelationError(message *diagnostics.Message, source *Type
return
}
}
message = diagnostics.Type_0_is_not_assignable_to_type_1

includesWideTypes := len(sourceType) > 30 && len(targetType) > 30
isPretty := r.c.compilerOptions.Pretty == core.TSTrue
if isPretty && includesWideTypes {
message = diagnostics.Type_Colon_0_is_not_assignable_to_type_Colon_1
} else {
message = diagnostics.Type_0_is_not_assignable_to_type_1
}
}
}
switch r.getChainMessage(0) {
Expand Down
4 changes: 4 additions & 0 deletions internal/diagnostics/diagnostics_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
prettyFormatLongTypes.ts:6:1 - error TS2882: Type:
{ b: { c: { e: { f: string; }; }; }; }

is not assignable to type:
{ b: { c: { e: { f: number; }; }; }; }

The types of 'b.c.e.f' are incompatible between these types.
Type 'string' is not assignable to type 'number'.

6 a = b;
  ~

prettyFormatLongTypes.ts:9:1 - error TS2322: Type 'string' is not assignable to type '{ b: { c: { e: { f: number; }; }; }; }'.

9 a = c;
  ~

prettyFormatLongTypes.ts:12:1 - error TS2322: Type '{ b: { c: { e: { f: number; }; }; }; }' is not assignable to type 'string'.

12 c = a;
  ~

prettyFormatLongTypes.ts:15:1 - error TS2322: Type 'boolean' is not assignable to type 'string'.

15 c = false;
  ~


==== prettyFormatLongTypes.ts (4 errors) ====
let a = { b: { c: { e: { f: 123 } } } };
let b = { b: { c: { e: { f: "123" } } } };
let c = "test";

// both the source and target types are wide enough enough trigger pretty printing
a = b;
~
!!! error TS2882: Type:
!!! error TS2882: { b: { c: { e: { f: string; }; }; }; }
!!! error TS2882:
!!! error TS2882: is not assignable to type:
!!! error TS2882: { b: { c: { e: { f: number; }; }; }; }
!!! error TS2882:
!!! error TS2882: The types of 'b.c.e.f' are incompatible between these types.
!!! error TS2882: Type 'string' is not assignable to type 'number'.

// only the source type is wide enough to trigger pretty printing
a = c;
~
!!! error TS2322: Type 'string' is not assignable to type '{ b: { c: { e: { f: number; }; }; }; }'.

// only the target type is wide enough to trigger pretty printing
c = a;
~
!!! error TS2322: Type '{ b: { c: { e: { f: number; }; }; }; }' is not assignable to type 'string'.

// neither the source nor the target type is wide enough to trigger pretty printing
c = false;
~
!!! error TS2322: Type 'boolean' is not assignable to type 'string'.

Found 4 errors in the same file, starting at: prettyFormatLongTypes.ts:6

32 changes: 32 additions & 0 deletions testdata/baselines/reference/compiler/prettyFormatLongTypes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//// [tests/cases/compiler/prettyFormatLongTypes.ts] ////

//// [prettyFormatLongTypes.ts]
let a = { b: { c: { e: { f: 123 } } } };
let b = { b: { c: { e: { f: "123" } } } };
let c = "test";

// both the source and target types are wide enough enough trigger pretty printing
a = b;

// only the source type is wide enough to trigger pretty printing
a = c;

// only the target type is wide enough to trigger pretty printing
c = a;

// neither the source nor the target type is wide enough to trigger pretty printing
c = false;


//// [prettyFormatLongTypes.js]
let a = { b: { c: { e: { f: 123 } } } };
let b = { b: { c: { e: { f: "123" } } } };
let c = "test";
// both the source and target types are wide enough enough trigger pretty printing
a = b;
// only the source type is wide enough to trigger pretty printing
a = c;
// only the target type is wide enough to trigger pretty printing
c = a;
// neither the source nor the target type is wide enough to trigger pretty printing
c = false;
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//// [tests/cases/compiler/prettyFormatLongTypes.ts] ////

=== prettyFormatLongTypes.ts ===
let a = { b: { c: { e: { f: 123 } } } };
>a : Symbol(a, Decl(prettyFormatLongTypes.ts, 0, 3))
>b : Symbol(b, Decl(prettyFormatLongTypes.ts, 0, 9))
>c : Symbol(c, Decl(prettyFormatLongTypes.ts, 0, 14))
>e : Symbol(e, Decl(prettyFormatLongTypes.ts, 0, 19))
>f : Symbol(f, Decl(prettyFormatLongTypes.ts, 0, 24))

let b = { b: { c: { e: { f: "123" } } } };
>b : Symbol(b, Decl(prettyFormatLongTypes.ts, 1, 3))
>b : Symbol(b, Decl(prettyFormatLongTypes.ts, 1, 9))
>c : Symbol(c, Decl(prettyFormatLongTypes.ts, 1, 14))
>e : Symbol(e, Decl(prettyFormatLongTypes.ts, 1, 19))
>f : Symbol(f, Decl(prettyFormatLongTypes.ts, 1, 24))

let c = "test";
>c : Symbol(c, Decl(prettyFormatLongTypes.ts, 2, 3))

// both the source and target types are wide enough enough trigger pretty printing
a = b;
>a : Symbol(a, Decl(prettyFormatLongTypes.ts, 0, 3))
>b : Symbol(b, Decl(prettyFormatLongTypes.ts, 1, 3))

// only the source type is wide enough to trigger pretty printing
a = c;
>a : Symbol(a, Decl(prettyFormatLongTypes.ts, 0, 3))
>c : Symbol(c, Decl(prettyFormatLongTypes.ts, 2, 3))

// only the target type is wide enough to trigger pretty printing
c = a;
>c : Symbol(c, Decl(prettyFormatLongTypes.ts, 2, 3))
>a : Symbol(a, Decl(prettyFormatLongTypes.ts, 0, 3))

// neither the source nor the target type is wide enough to trigger pretty printing
c = false;
>c : Symbol(c, Decl(prettyFormatLongTypes.ts, 2, 3))

55 changes: 55 additions & 0 deletions testdata/baselines/reference/compiler/prettyFormatLongTypes.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//// [tests/cases/compiler/prettyFormatLongTypes.ts] ////

=== prettyFormatLongTypes.ts ===
let a = { b: { c: { e: { f: 123 } } } };
>a : { b: { c: { e: { f: number; }; }; }; }
>{ b: { c: { e: { f: 123 } } } } : { b: { c: { e: { f: number; }; }; }; }
>b : { c: { e: { f: number; }; }; }
>{ c: { e: { f: 123 } } } : { c: { e: { f: number; }; }; }
>c : { e: { f: number; }; }
>{ e: { f: 123 } } : { e: { f: number; }; }
>e : { f: number; }
>{ f: 123 } : { f: number; }
>f : number
>123 : 123

let b = { b: { c: { e: { f: "123" } } } };
>b : { b: { c: { e: { f: string; }; }; }; }
>{ b: { c: { e: { f: "123" } } } } : { b: { c: { e: { f: string; }; }; }; }
>b : { c: { e: { f: string; }; }; }
>{ c: { e: { f: "123" } } } : { c: { e: { f: string; }; }; }
>c : { e: { f: string; }; }
>{ e: { f: "123" } } : { e: { f: string; }; }
>e : { f: string; }
>{ f: "123" } : { f: string; }
>f : string
>"123" : "123"

let c = "test";
>c : string
>"test" : "test"

// both the source and target types are wide enough enough trigger pretty printing
a = b;
>a = b : { b: { c: { e: { f: string; }; }; }; }
>a : { b: { c: { e: { f: number; }; }; }; }
>b : { b: { c: { e: { f: string; }; }; }; }

// only the source type is wide enough to trigger pretty printing
a = c;
>a = c : string
>a : { b: { c: { e: { f: number; }; }; }; }
>c : string

// only the target type is wide enough to trigger pretty printing
c = a;
>c = a : { b: { c: { e: { f: number; }; }; }; }
>c : string
>a : { b: { c: { e: { f: number; }; }; }; }

// neither the source nor the target type is wide enough to trigger pretty printing
c = false;
>c = false : false
>c : string
>false : false

17 changes: 17 additions & 0 deletions testdata/tests/cases/compiler/prettyFormatLongTypes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// @pretty: true

let a = { b: { c: { e: { f: 123 } } } };
let b = { b: { c: { e: { f: "123" } } } };
let c = "test";

// both the source and target types are wide enough enough trigger pretty printing
a = b;

// only the source type is wide enough to trigger pretty printing
a = c;

// only the target type is wide enough to trigger pretty printing
c = a;

// neither the source nor the target type is wide enough to trigger pretty printing
c = false;