-
Notifications
You must be signed in to change notification settings - Fork 13.4k
feat(45679): support 'did you mean' diagnostics for string literal union #45723
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
sandersn
merged 3 commits into
microsoft:main
from
hi-ogawa:45679-did-you-mean-string-literal-union
Sep 14, 2021
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -17740,6 +17740,13 @@ namespace ts { | |||||
| message = Diagnostics.Type_0_is_not_assignable_to_type_1_with_exactOptionalPropertyTypes_Colon_true_Consider_adding_undefined_to_the_types_of_the_target_s_properties; | ||||||
| } | ||||||
| else { | ||||||
| if (source.flags & TypeFlags.StringLiteral && target.flags & TypeFlags.Union) { | ||||||
| const suggestedType = getSuggestedTypeForNonexistentStringLiteralType(source as StringLiteralType, target as UnionType); | ||||||
| if (suggestedType) { | ||||||
| reportError(Diagnostics.Type_0_is_not_assignable_to_type_1_Did_you_mean_2, generalizedSourceType, targetType, typeToString(suggestedType)); | ||||||
| return; | ||||||
| } | ||||||
| } | ||||||
| message = Diagnostics.Type_0_is_not_assignable_to_type_1; | ||||||
| } | ||||||
| } | ||||||
|
|
@@ -28127,6 +28134,11 @@ namespace ts { | |||||
| return suggestion; | ||||||
| } | ||||||
|
|
||||||
| function getSuggestedTypeForNonexistentStringLiteralType(source: StringLiteralType, target: UnionType): StringLiteralType | undefined { | ||||||
| const candidates = target.types.filter((type): type is StringLiteralType => !!(type.flags & TypeFlags.StringLiteral)); | ||||||
| return getSpellingSuggestion(source.value, candidates, type => type.value); | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. very minor: no parens around arrow parameter
Suggested change
|
||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Given a name and a list of symbols whose names are *not* equal to the name, return a spelling suggestion if there is one that is close enough. | ||||||
| * Names less than length 3 only check for case-insensitive equality, not levenshtein distance. | ||||||
|
|
||||||
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
20 changes: 20 additions & 0 deletions
20
tests/baselines/reference/didYouMeanStringLiteral.errors.txt
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| tests/cases/compiler/didYouMeanStringLiteral.ts(5,7): error TS2820: Type '"strong"' is not assignable to type 'T1'. Did you mean '"string"'? | ||
| tests/cases/compiler/didYouMeanStringLiteral.ts(6,7): error TS2322: Type '"strong"' is not assignable to type '"number" | "boolean"'. | ||
| tests/cases/compiler/didYouMeanStringLiteral.ts(7,7): error TS2820: Type '"strong"' is not assignable to type '"string" | "boolean"'. Did you mean '"string"'? | ||
|
|
||
|
|
||
| ==== tests/cases/compiler/didYouMeanStringLiteral.ts (3 errors) ==== | ||
| type T1 = "string" | "number" | "boolean"; | ||
| type T2 = T1 & ("number" | "boolean"); // "number" | "boolean" | ||
| type T3 = T1 & ("string" | "boolean"); // "string" | "boolean" | ||
|
|
||
| const t1: T1 = "strong"; | ||
| ~~ | ||
| !!! error TS2820: Type '"strong"' is not assignable to type 'T1'. Did you mean '"string"'? | ||
| const t2: T2 = "strong"; | ||
| ~~ | ||
| !!! error TS2322: Type '"strong"' is not assignable to type '"number" | "boolean"'. | ||
| const t3: T3 = "strong"; | ||
| ~~ | ||
| !!! error TS2820: Type '"strong"' is not assignable to type '"string" | "boolean"'. Did you mean '"string"'? | ||
|
|
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| //// [didYouMeanStringLiteral.ts] | ||
| type T1 = "string" | "number" | "boolean"; | ||
| type T2 = T1 & ("number" | "boolean"); // "number" | "boolean" | ||
| type T3 = T1 & ("string" | "boolean"); // "string" | "boolean" | ||
|
|
||
| const t1: T1 = "strong"; | ||
| const t2: T2 = "strong"; | ||
| const t3: T3 = "strong"; | ||
|
|
||
|
|
||
| //// [didYouMeanStringLiteral.js] | ||
| var t1 = "strong"; | ||
| var t2 = "strong"; | ||
| var t3 = "strong"; |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| === tests/cases/compiler/didYouMeanStringLiteral.ts === | ||
| type T1 = "string" | "number" | "boolean"; | ||
| >T1 : Symbol(T1, Decl(didYouMeanStringLiteral.ts, 0, 0)) | ||
|
|
||
| type T2 = T1 & ("number" | "boolean"); // "number" | "boolean" | ||
| >T2 : Symbol(T2, Decl(didYouMeanStringLiteral.ts, 0, 42)) | ||
| >T1 : Symbol(T1, Decl(didYouMeanStringLiteral.ts, 0, 0)) | ||
|
|
||
| type T3 = T1 & ("string" | "boolean"); // "string" | "boolean" | ||
| >T3 : Symbol(T3, Decl(didYouMeanStringLiteral.ts, 1, 38)) | ||
| >T1 : Symbol(T1, Decl(didYouMeanStringLiteral.ts, 0, 0)) | ||
|
|
||
| const t1: T1 = "strong"; | ||
| >t1 : Symbol(t1, Decl(didYouMeanStringLiteral.ts, 4, 5)) | ||
| >T1 : Symbol(T1, Decl(didYouMeanStringLiteral.ts, 0, 0)) | ||
|
|
||
| const t2: T2 = "strong"; | ||
| >t2 : Symbol(t2, Decl(didYouMeanStringLiteral.ts, 5, 5)) | ||
| >T2 : Symbol(T2, Decl(didYouMeanStringLiteral.ts, 0, 42)) | ||
|
|
||
| const t3: T3 = "strong"; | ||
| >t3 : Symbol(t3, Decl(didYouMeanStringLiteral.ts, 6, 5)) | ||
| >T3 : Symbol(T3, Decl(didYouMeanStringLiteral.ts, 1, 38)) | ||
|
|
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| === tests/cases/compiler/didYouMeanStringLiteral.ts === | ||
| type T1 = "string" | "number" | "boolean"; | ||
| >T1 : T1 | ||
|
|
||
| type T2 = T1 & ("number" | "boolean"); // "number" | "boolean" | ||
| >T2 : "number" | "boolean" | ||
|
|
||
| type T3 = T1 & ("string" | "boolean"); // "string" | "boolean" | ||
| >T3 : "string" | "boolean" | ||
|
|
||
| const t1: T1 = "strong"; | ||
| >t1 : T1 | ||
| >"strong" : "strong" | ||
|
|
||
| const t2: T2 = "strong"; | ||
| >t2 : "number" | "boolean" | ||
| >"strong" : "strong" | ||
|
|
||
| const t3: T3 = "strong"; | ||
| >t3 : "string" | "boolean" | ||
| >"strong" : "strong" | ||
|
|
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| type T1 = "string" | "number" | "boolean"; | ||
| type T2 = T1 & ("number" | "boolean"); // "number" | "boolean" | ||
| type T3 = T1 & ("string" | "boolean"); // "string" | "boolean" | ||
|
|
||
| const t1: T1 = "strong"; | ||
| const t2: T2 = "strong"; | ||
| const t3: T3 = "strong"; |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently,
generalizedSourceTypemust=== sourceTypein this case, but it would be better to usegeneralizedSourceTypehere, like the otherreportErrors, in case we add some additional processing at the beginning of the function.