Fix duplicate error message for literal union types#63180
Open
erdinccurebal wants to merge 1 commit intomicrosoft:mainfrom
Open
Fix duplicate error message for literal union types#63180erdinccurebal wants to merge 1 commit intomicrosoft:mainfrom
erdinccurebal wants to merge 1 commit intomicrosoft:mainfrom
Conversation
When a non-enum union of literals (e.g. "a" | "b") is assigned to an incompatible type, the error would generalize the union to its base type (e.g. string), producing a duplicate of the per-member elaboration message. Now, when the generalized form collapses to a single non-union base type, the original literal union is shown instead.
Author
|
@microsoft-github-policy-service agree |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #63050
Problem
When assigning a non-enum union of string/number/bigint literals to an incompatible type, the error message would display the same information twice. For example:
Previously produced:
The union
"a" | "b"was generalized tostring, which is identical to the per-member elaboration (each member also generalizes tostring), resulting in a duplicate message.Fix
In
reportRelationError, after computing the generalized base type, check whether the source is a non-enum union whose members all share the same base type (i.e., the generalized form is a single non-union type). In that case, skip the generalization for display purposes so the error shows the original literal union instead:The two lines now provide complementary information: the first shows what the type is, the second shows why it's incompatible.
Unions with mixed base types (e.g.
1 | ""→number | string) are still generalized as before, since the generalized form differs from any single member's elaboration.Baseline changes
Three existing baselines are updated — all had duplicate messages that are now fixed:
typeof xresult union was generalized tostring, duplicating the elaboration6n | 7n | 8n(type aliasQ) was generalized tobigint, duplicating the elaboration"text" | "email"was generalized tostring, duplicating the elaboration