Skip to content

Added new diagnostics message to clarify error for type guards #7348

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
merged 1 commit into from
Mar 2, 2016
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
5 changes: 4 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11815,9 +11815,12 @@ namespace ts {
Diagnostics.A_type_predicate_cannot_reference_a_rest_parameter);
}
else {
const leadingError = chainDiagnosticMessages(undefined, Diagnostics.A_type_predicate_s_type_must_be_assignable_to_its_parameter_s_type);
checkTypeAssignableTo(typePredicate.type,
getTypeOfNode(parent.parameters[typePredicate.parameterIndex]),
node.type);
node.type,
/*headMessage*/ undefined,
leadingError);
}
}
else if (parameterName) {
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -1847,6 +1847,10 @@
"category": "Error",
"code": 2676
},
"A type predicate's type must be assignable to its parameter's type.": {
"category": "Error",
"code": 2677
},
"Import declaration '{0}' is using private name '{1}'.": {
"category": "Error",
"code": 4000
Expand Down
22 changes: 14 additions & 8 deletions tests/baselines/reference/typeGuardFunctionErrors.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@ tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(31,5):
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(31,5): error TS7027: Unreachable code detected.
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(32,1): error TS1128: Declaration or statement expected.
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(34,38): error TS1225: Cannot find parameter 'x'.
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(38,51): error TS2322: Type 'B' is not assignable to type 'A'.
Property 'propA' is missing in type 'B'.
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(42,56): error TS2322: Type 'number' is not assignable to type 'string'.
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(46,56): error TS2322: Type 'T[]' is not assignable to type 'string'.
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(38,51): error TS2677: A type predicate's type must be assignable to its parameter's type.
Type 'B' is not assignable to type 'A'.
Property 'propA' is missing in type 'B'.
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(42,56): error TS2677: A type predicate's type must be assignable to its parameter's type.
Type 'number' is not assignable to type 'string'.
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(46,56): error TS2677: A type predicate's type must be assignable to its parameter's type.
Type 'T[]' is not assignable to type 'string'.
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(60,7): error TS2339: Property 'propB' does not exist on type 'A'.
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(65,7): error TS2339: Property 'propB' does not exist on type 'A'.
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(70,7): error TS2339: Property 'propB' does not exist on type 'A'.
Expand Down Expand Up @@ -129,20 +132,23 @@ tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(137,39

function hasNonMatchingParameterType1(x: A): x is B {
~
!!! error TS2322: Type 'B' is not assignable to type 'A'.
!!! error TS2322: Property 'propA' is missing in type 'B'.
!!! error TS2677: A type predicate's type must be assignable to its parameter's type.
!!! error TS2677: Type 'B' is not assignable to type 'A'.
!!! error TS2677: Property 'propA' is missing in type 'B'.
return true;
}

function hasNonMatchingParameterType2(x: string): x is number {
~~~~~~
!!! error TS2322: Type 'number' is not assignable to type 'string'.
!!! error TS2677: A type predicate's type must be assignable to its parameter's type.
!!! error TS2677: Type 'number' is not assignable to type 'string'.
return true;
}

function hasNonMathcingGenericType<T>(a: string): a is T[] {
~~~
!!! error TS2322: Type 'T[]' is not assignable to type 'string'.
!!! error TS2677: A type predicate's type must be assignable to its parameter's type.
!!! error TS2677: Type 'T[]' is not assignable to type 'string'.
return true;
}

Expand Down