Skip to content

Improve the error message when asserting to a type that is not comparable to the original. - #25541

Merged
Daniel Rosenwasser (DanielRosenwasser) merged 1 commit into
microsoft:masterfrom
mattmccutchen:issue-25539
Jul 25, 2018
Merged

Daniel Rosenwasser (DanielRosenwasser) merged 1 commit into
microsoft:masterfrom
mattmccutchen:issue-25539

Conversation

@mattmccutchen

@mattmccutchen Matt McCutchen (mattmccutchen) commented Jul 10, 2018 •

Copy link
Copy Markdown
Contributor

Also improve the error message for implicit conversion of a symbol to a string in a template literal, which previously shared the error message with type assertions.

Fixes #25539. Addresses #25870.

Comment thread src/compiler/checker.ts Outdated
if (!isTypeComparableTo(targetType, widenedType)) {
checkTypeComparableTo(exprType, targetType, errNode, Diagnostics.Type_0_cannot_be_converted_to_type_1);
checkTypeComparableTo(exprType, targetType, errNode,
Diagnostics.Type_0_cannot_be_converted_directly_to_type_1_because_it_is_neither_a_supertype_nor_a_subtype_of_1_If_this_is_intentional_convert_to_unknown_first);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's really not only about supertype vs. subtype: we have these relationships for subtyping, assignability, and comparability). Comparability has some other properties that are technically more lax.

The comparability and assignment compatibility rules differ only in that

  • a union type is comparable to a target type if any of its constituents, rather than all of its constituents, are comparable to that target type,
  • whether a type is considered weak (3.11.7) has no bearing on whether two types are comparable,
  • whether an object type has excess properties (3.11.8) has no bearing on whether two types are comparable,
  • an object type with some optional property is comparable to an object type containing a property of the same name if it has a compatible type, regardless of whether that property is optional, and
  • when relating any two signatures, each signature is always instantiated using type Any for all type arguments.

So maybe you want something like

Type '{0}' cannot be converted to type '{1}' because neither type sufficiently overlaps with the other.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Daniel Rosenwasser (@DanielRosenwasser) worth reading the discussion in the linked issue IMO

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comparability and assignment compatibility rules differ only in that [...]

Where is this secret more-up-to-date version of the language spec? Aha, web search is my friend.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To try to move this forward, I went with the "neither type sufficiently overlaps with the other" wording.

@mattmccutchen
Matt McCutchen (mattmccutchen) force-pushed the issue-25539 branch 2 times, most recently from 1c675dd to f1fabd8 Compare July 17, 2018 20:26
@mattmccutchen Matt McCutchen (mattmccutchen) changed the title Improve the error message when asserting to a type that is neither a supertype nor a subtype of the original. Improve the error message when asserting to a type that is not comparable to the original. Jul 17, 2018
@mhegazy

Copy link
Copy Markdown
Contributor

Daniel Rosenwasser (@DanielRosenwasser) can you take another look please.

Comment thread src/compiler/diagnosticMessages.json Outdated
"category": "Error",
"code": 2729
},
"Implicit conversion of a symbol to a string will fail at runtime.": {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you wrap symbol and string in single-quotes so our localization team knows not to translate them? Also, can you add a suggestion at the end like "Consider wrapping this expression in 'String(...)' if this is intentional."

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Comment thread src/compiler/diagnosticMessages.json Outdated
"code": 2351
},
"Type '{0}' cannot be converted to type '{1}'.": {
"Conversion of type '{0}' to type '{1}' may be a mistake because neither type sufficiently overlaps with the other. If it is intended, convert to 'unknown' first.": {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Final nit: can you make this "If this was intentional, convert the expression to 'unknown' first."

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

comparable to the original.

Also improve the error message for implicit conversion of a symbol to a
string in a template literal, which previously shared the error message
with type assertions.

Fixes microsoft#25539.  Addresses microsoft#25870.
@DanielRosenwasser

Copy link
Copy Markdown
Member

Thanks!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants