Skip to content

Commit 8ae7c6c

Browse files
committed
Fix type predicate index in error reporting
Previously type predicate error checking didn't take the `this` parameter into account when checking for errors. This led to spurious errors. Note that it's not feasible to change the initial value of `parameterIndex` because several checks refer to the original declaration, for example to make sure that a type predicate doesn't refer to a rest parameter.
1 parent e1ba65a commit 8ae7c6c

1 file changed

Lines changed: 11 additions & 6 deletions

File tree

src/compiler/checker.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8592,7 +8592,7 @@ namespace ts {
85928592
// The following block preserves behavior forbidding boolean returning functions from being assignable to type guard returning functions
85938593
if (target.typePredicate) {
85948594
if (source.typePredicate) {
8595-
result &= compareTypePredicateRelatedTo(source.typePredicate, target.typePredicate, reportErrors, errorReporter, compareTypes);
8595+
result &= compareTypePredicateRelatedTo(source.typePredicate, target.typePredicate, source.declaration, target.declaration, reportErrors, errorReporter, compareTypes);
85968596
}
85978597
else if (isIdentifierTypePredicate(target.typePredicate)) {
85988598
if (reportErrors) {
@@ -8614,8 +8614,11 @@ namespace ts {
86148614
return result;
86158615
}
86168616

8617-
function compareTypePredicateRelatedTo(source: TypePredicate,
8617+
function compareTypePredicateRelatedTo(
8618+
source: TypePredicate,
86188619
target: TypePredicate,
8620+
sourceDeclaration: SignatureDeclaration,
8621+
targetDeclaration: SignatureDeclaration,
86198622
reportErrors: boolean,
86208623
errorReporter: ErrorReporter,
86218624
compareTypes: (s: Type, t: Type, reportErrors?: boolean) => Ternary): Ternary {
@@ -8628,11 +8631,13 @@ namespace ts {
86288631
}
86298632

86308633
if (source.kind === TypePredicateKind.Identifier) {
8631-
const sourceIdentifierPredicate = source as IdentifierTypePredicate;
8632-
const targetIdentifierPredicate = target as IdentifierTypePredicate;
8633-
if (sourceIdentifierPredicate.parameterIndex !== targetIdentifierPredicate.parameterIndex) {
8634+
const sourcePredicate = source as IdentifierTypePredicate;
8635+
const targetPredicate = target as IdentifierTypePredicate;
8636+
const sourceIndex = sourcePredicate.parameterIndex - (getThisParameter(sourceDeclaration) ? 1 : 0);
8637+
const targetIndex = targetPredicate.parameterIndex - (getThisParameter(targetDeclaration) ? 1 : 0);
8638+
if (sourceIndex !== targetIndex) {
86348639
if (reportErrors) {
8635-
errorReporter(Diagnostics.Parameter_0_is_not_in_the_same_position_as_parameter_1, sourceIdentifierPredicate.parameterName, targetIdentifierPredicate.parameterName);
8640+
errorReporter(Diagnostics.Parameter_0_is_not_in_the_same_position_as_parameter_1, sourcePredicate.parameterName, targetPredicate.parameterName);
86368641
errorReporter(Diagnostics.Type_predicate_0_is_not_assignable_to_1, typePredicateToString(source), typePredicateToString(target));
86378642
}
86388643
return Ternary.False;

0 commit comments

Comments
 (0)