Skip to content

Commit d6c5606

Browse files
Merge pull request #7348 from Vinatorul/issue6540
Added new diagnostics message to clarify error for type guards
2 parents de47fcc + 694a48c commit d6c5606

File tree

3 files changed

+22
-9
lines changed

3 files changed

+22
-9
lines changed

src/compiler/checker.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11815,9 +11815,12 @@ namespace ts {
1181511815
Diagnostics.A_type_predicate_cannot_reference_a_rest_parameter);
1181611816
}
1181711817
else {
11818+
const leadingError = chainDiagnosticMessages(undefined, Diagnostics.A_type_predicate_s_type_must_be_assignable_to_its_parameter_s_type);
1181811819
checkTypeAssignableTo(typePredicate.type,
1181911820
getTypeOfNode(parent.parameters[typePredicate.parameterIndex]),
11820-
node.type);
11821+
node.type,
11822+
/*headMessage*/ undefined,
11823+
leadingError);
1182111824
}
1182211825
}
1182311826
else if (parameterName) {

src/compiler/diagnosticMessages.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1847,6 +1847,10 @@
18471847
"category": "Error",
18481848
"code": 2676
18491849
},
1850+
"A type predicate's type must be assignable to its parameter's type.": {
1851+
"category": "Error",
1852+
"code": 2677
1853+
},
18501854
"Import declaration '{0}' is using private name '{1}'.": {
18511855
"category": "Error",
18521856
"code": 4000

tests/baselines/reference/typeGuardFunctionErrors.errors.txt

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,13 @@ tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(31,5):
1212
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(31,5): error TS7027: Unreachable code detected.
1313
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(32,1): error TS1128: Declaration or statement expected.
1414
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(34,38): error TS1225: Cannot find parameter 'x'.
15-
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(38,51): error TS2322: Type 'B' is not assignable to type 'A'.
16-
Property 'propA' is missing in type 'B'.
17-
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(42,56): error TS2322: Type 'number' is not assignable to type 'string'.
18-
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(46,56): error TS2322: Type 'T[]' is not assignable to type 'string'.
15+
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(38,51): error TS2677: A type predicate's type must be assignable to its parameter's type.
16+
Type 'B' is not assignable to type 'A'.
17+
Property 'propA' is missing in type 'B'.
18+
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(42,56): error TS2677: A type predicate's type must be assignable to its parameter's type.
19+
Type 'number' is not assignable to type 'string'.
20+
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(46,56): error TS2677: A type predicate's type must be assignable to its parameter's type.
21+
Type 'T[]' is not assignable to type 'string'.
1922
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(60,7): error TS2339: Property 'propB' does not exist on type 'A'.
2023
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(65,7): error TS2339: Property 'propB' does not exist on type 'A'.
2124
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(70,7): error TS2339: Property 'propB' does not exist on type 'A'.
@@ -129,20 +132,23 @@ tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(137,39
129132

130133
function hasNonMatchingParameterType1(x: A): x is B {
131134
~
132-
!!! error TS2322: Type 'B' is not assignable to type 'A'.
133-
!!! error TS2322: Property 'propA' is missing in type 'B'.
135+
!!! error TS2677: A type predicate's type must be assignable to its parameter's type.
136+
!!! error TS2677: Type 'B' is not assignable to type 'A'.
137+
!!! error TS2677: Property 'propA' is missing in type 'B'.
134138
return true;
135139
}
136140

137141
function hasNonMatchingParameterType2(x: string): x is number {
138142
~~~~~~
139-
!!! error TS2322: Type 'number' is not assignable to type 'string'.
143+
!!! error TS2677: A type predicate's type must be assignable to its parameter's type.
144+
!!! error TS2677: Type 'number' is not assignable to type 'string'.
140145
return true;
141146
}
142147

143148
function hasNonMathcingGenericType<T>(a: string): a is T[] {
144149
~~~
145-
!!! error TS2322: Type 'T[]' is not assignable to type 'string'.
150+
!!! error TS2677: A type predicate's type must be assignable to its parameter's type.
151+
!!! error TS2677: Type 'T[]' is not assignable to type 'string'.
146152
return true;
147153
}
148154

0 commit comments

Comments
 (0)