Skip to content
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
12 changes: 12 additions & 0 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
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.

Currently, generalizedSourceType must === sourceType in this case, but it would be better to use generalizedSourceType here, like the other reportErrors, in case we add some additional processing at the beginning of the function.

Suggested change
reportError(Diagnostics.Type_0_is_not_assignable_to_type_1_Did_you_mean_2, sourceType, targetType, typeToString(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;
}
}
Expand Down Expand Up @@ -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);
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.

very minor: no parens around arrow parameter

Suggested change
return getSpellingSuggestion(source.value, candidates, (type) => type.value);
return getSpellingSuggestion(source.value, candidates, type => type.value);

}

/**
* 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.
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -3284,6 +3284,10 @@
"category": "Error",
"code": 2819
},
"Type '{0}' is not assignable to type '{1}'. Did you mean '{2}'?": {
"category": "Error",
"code": 2820
},

"Import declaration '{0}' is using private name '{1}'.": {
"category": "Error",
Expand Down
20 changes: 20 additions & 0 deletions tests/baselines/reference/didYouMeanStringLiteral.errors.txt
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"'?

14 changes: 14 additions & 0 deletions tests/baselines/reference/didYouMeanStringLiteral.js
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";
24 changes: 24 additions & 0 deletions tests/baselines/reference/didYouMeanStringLiteral.symbols
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))

22 changes: 22 additions & 0 deletions tests/baselines/reference/didYouMeanStringLiteral.types
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"

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
tests/cases/compiler/errorsForCallAndAssignmentAreSimilar.ts(11,11): error TS2322: Type '"hdpvd"' is not assignable to type '"hddvd" | "bluray"'.
tests/cases/compiler/errorsForCallAndAssignmentAreSimilar.ts(16,11): error TS2322: Type '"hdpvd"' is not assignable to type '"hddvd" | "bluray"'.
tests/cases/compiler/errorsForCallAndAssignmentAreSimilar.ts(11,11): error TS2820: Type '"hdpvd"' is not assignable to type '"hddvd" | "bluray"'. Did you mean '"hddvd"'?
tests/cases/compiler/errorsForCallAndAssignmentAreSimilar.ts(16,11): error TS2820: Type '"hdpvd"' is not assignable to type '"hddvd" | "bluray"'. Did you mean '"hddvd"'?


==== tests/cases/compiler/errorsForCallAndAssignmentAreSimilar.ts (2 errors) ====
Expand All @@ -15,15 +15,15 @@ tests/cases/compiler/errorsForCallAndAssignmentAreSimilar.ts(16,11): error TS232
{ kind: "bluray", },
{ kind: "hdpvd", }
~~~~
!!! error TS2322: Type '"hdpvd"' is not assignable to type '"hddvd" | "bluray"'.
!!! error TS2820: Type '"hdpvd"' is not assignable to type '"hddvd" | "bluray"'. Did you mean '"hddvd"'?
!!! related TS6500 tests/cases/compiler/errorsForCallAndAssignmentAreSimilar.ts:3:13: The expected type comes from property 'kind' which is declared here on type 'Disc'
]);

const ds: Disc[] = [
{ kind: "bluray", },
{ kind: "hdpvd", }
~~~~
!!! error TS2322: Type '"hdpvd"' is not assignable to type '"hddvd" | "bluray"'.
!!! error TS2820: Type '"hdpvd"' is not assignable to type '"hddvd" | "bluray"'. Did you mean '"hddvd"'?
!!! related TS6500 tests/cases/compiler/errorsForCallAndAssignmentAreSimilar.ts:3:13: The expected type comes from property 'kind' which is declared here on type 'Disc'
];
}
7 changes: 7 additions & 0 deletions tests/cases/compiler/didYouMeanStringLiteral.ts
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";