Skip to content

Fix error display on literal types #429

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

Merged
merged 2 commits into from
Mar 8, 2025
Merged
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
8 changes: 4 additions & 4 deletions internal/checker/relater.go
Original file line number Diff line number Diff line change
Expand Up @@ -4699,24 +4699,24 @@ func (r *Relater) reportRelationError(message *diagnostics.Message, source *Type
case diagnostics.Excessive_complexity_comparing_types_0_and_1,
diagnostics.Excessive_stack_depth_comparing_types_0_and_1,
diagnostics.The_type_0_is_readonly_and_cannot_be_assigned_to_the_mutable_type_1:
if r.chainArgsMatch(sourceType, targetType) {
if r.chainArgsMatch(generalizedSourceType, targetType) {
return
}
// Suppress if next message is a missing property message for source and target and we're not
// reporting on interface implementation
case diagnostics.Property_0_is_missing_in_type_1_but_required_in_type_2:
if !isInterfaceImplementationMessage(message) && r.chainArgsMatch(nil, sourceType, targetType) {
if !isInterfaceImplementationMessage(message) && r.chainArgsMatch(nil, generalizedSourceType, targetType) {
return
}
// Suppress if next message is a missing property message for source and target and we're not
// reporting on interface implementation
case diagnostics.Type_0_is_missing_the_following_properties_from_type_1_Colon_2_and_3_more,
diagnostics.Type_0_is_missing_the_following_properties_from_type_1_Colon_2:
if !isInterfaceImplementationMessage(message) && r.chainArgsMatch(sourceType, targetType) {
if !isInterfaceImplementationMessage(message) && r.chainArgsMatch(generalizedSourceType, targetType) {
return
}
}
r.reportError(message, sourceType, targetType)
r.reportError(message, generalizedSourceType, targetType)
}

func (r *Relater) reportError(message *diagnostics.Message, args ...any) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/src/bar.ts(1,7): error TS2322: Type '1' is not assignable to type 'string'.
/src/foo.ts(1,7): error TS2322: Type '""' is not assignable to type 'number'.
/src/bar.ts(1,7): error TS2322: Type 'number' is not assignable to type 'string'.
/src/foo.ts(1,7): error TS2322: Type 'string' is not assignable to type 'number'.


==== /src/foo.ts (1 errors) ====
const x: number = "";
~
!!! error TS2322: Type '""' is not assignable to type 'number'.
!!! error TS2322: Type 'string' is not assignable to type 'number'.

==== /src/bar.ts (1 errors) ====
const y: string = 1;
~
!!! error TS2322: Type '1' is not assignable to type 'string'.
!!! error TS2322: Type 'number' is not assignable to type 'string'.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
simpleTestSingleFile.ts(1,7): error TS2322: Type '""' is not assignable to type 'number'.
simpleTestSingleFile.ts(1,7): error TS2322: Type 'string' is not assignable to type 'number'.


==== simpleTestSingleFile.ts (1 errors) ====
const x: number = "";
~
!!! error TS2322: Type '""' is not assignable to type 'number'.
!!! error TS2322: Type 'string' is not assignable to type 'number'.
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ CompilerOptions::{


Output::
a.ts(1,7): error TS2322: Type '"hello"' is not assignable to type 'number'.
a.ts(1,7): error TS2322: Type 'string' is not assignable to type 'number'.


Found 1 error in a.ts:1
Expand Down Expand Up @@ -194,7 +194,7 @@ Output::
Edit:: introduce error

Output::
a.ts(1,7): error TS2322: Type '"hello"' is not assignable to type 'number'.
a.ts(1,7): error TS2322: Type 'string' is not assignable to type 'number'.


Found 1 error in a.ts:1
Expand All @@ -209,7 +209,7 @@ const a: number = "hello"
Edit:: emit when error

Output::
a.ts(1,7): error TS2322: Type '"hello"' is not assignable to type 'number'.
a.ts(1,7): error TS2322: Type 'string' is not assignable to type 'number'.


Found 1 error in a.ts:1
Expand All @@ -228,7 +228,7 @@ Found 1 error in a.ts:1
Edit:: no emit run when error

Output::
a.ts(1,7): error TS2322: Type '"hello"' is not assignable to type 'number'.
a.ts(1,7): error TS2322: Type 'string' is not assignable to type 'number'.


Found 1 error in a.ts:1
Expand Down