@@ -8295,38 +8295,28 @@ namespace ts {
8295
8295
}
8296
8296
}
8297
8297
signature.resolvedTypePredicate = type && isTypePredicateNode(type) ?
8298
- createTypePredicateFromTypePredicateNode(type, signature.declaration! ) :
8298
+ createTypePredicateFromTypePredicateNode(type, signature) :
8299
8299
jsdocPredicate || noTypePredicate;
8300
8300
}
8301
8301
Debug.assert(!!signature.resolvedTypePredicate);
8302
8302
}
8303
8303
return signature.resolvedTypePredicate === noTypePredicate ? undefined : signature.resolvedTypePredicate;
8304
8304
}
8305
8305
8306
- function createTypePredicateFromTypePredicateNode(node: TypePredicateNode, func: SignatureDeclaration | JSDocSignature ): IdentifierTypePredicate | ThisTypePredicate {
8306
+ function createTypePredicateFromTypePredicateNode(node: TypePredicateNode, signature: Signature ): IdentifierTypePredicate | ThisTypePredicate {
8307
8307
const { parameterName } = node;
8308
8308
const type = getTypeFromTypeNode(node.type);
8309
8309
if (parameterName.kind === SyntaxKind.Identifier) {
8310
8310
return createIdentifierTypePredicate(
8311
8311
parameterName.escapedText as string,
8312
- getTypePredicateParameterIndex(func .parameters, parameterName),
8312
+ findIndex(signature .parameters, p => p.escapedName === parameterName.escapedText ),
8313
8313
type);
8314
8314
}
8315
8315
else {
8316
8316
return createThisTypePredicate(type);
8317
8317
}
8318
8318
}
8319
8319
8320
- function getTypePredicateParameterIndex(parameterList: ReadonlyArray<ParameterDeclaration | JSDocParameterTag>, parameter: Identifier): number {
8321
- for (let i = 0; i < parameterList.length; i++) {
8322
- const param = parameterList[i];
8323
- if (param.name.kind === SyntaxKind.Identifier && param.name.escapedText === parameter.escapedText) {
8324
- return i;
8325
- }
8326
- }
8327
- return -1;
8328
- }
8329
-
8330
8320
function getReturnTypeOfSignature(signature: Signature): Type {
8331
8321
if (!signature.resolvedReturnType) {
8332
8322
if (!pushTypeResolution(signature, TypeSystemPropertyName.ResolvedReturnType)) {
@@ -11808,7 +11798,7 @@ namespace ts {
11808
11798
if (targetTypePredicate) {
11809
11799
const sourceTypePredicate = getTypePredicateOfSignature(source);
11810
11800
if (sourceTypePredicate) {
11811
- result &= compareTypePredicateRelatedTo(sourceTypePredicate, targetTypePredicate, source.declaration!, target.declaration!, reportErrors, errorReporter, compareTypes); // TODO: GH#18217
11801
+ result &= compareTypePredicateRelatedTo(sourceTypePredicate, targetTypePredicate, reportErrors, errorReporter, compareTypes);
11812
11802
}
11813
11803
else if (isIdentifierTypePredicate(targetTypePredicate)) {
11814
11804
if (reportErrors) {
@@ -11833,8 +11823,6 @@ namespace ts {
11833
11823
function compareTypePredicateRelatedTo(
11834
11824
source: TypePredicate,
11835
11825
target: TypePredicate,
11836
- sourceDeclaration: SignatureDeclaration | JSDocSignature,
11837
- targetDeclaration: SignatureDeclaration | JSDocSignature,
11838
11826
reportErrors: boolean,
11839
11827
errorReporter: ErrorReporter | undefined,
11840
11828
compareTypes: (s: Type, t: Type, reportErrors?: boolean) => Ternary): Ternary {
@@ -11847,12 +11835,9 @@ namespace ts {
11847
11835
}
11848
11836
11849
11837
if (source.kind === TypePredicateKind.Identifier) {
11850
- const targetPredicate = target as IdentifierTypePredicate;
11851
- const sourceIndex = source.parameterIndex - (getThisParameter(sourceDeclaration) ? 1 : 0);
11852
- const targetIndex = targetPredicate.parameterIndex - (getThisParameter(targetDeclaration) ? 1 : 0);
11853
- if (sourceIndex !== targetIndex) {
11838
+ if (source.parameterIndex !== (target as IdentifierTypePredicate).parameterIndex) {
11854
11839
if (reportErrors) {
11855
- errorReporter!(Diagnostics.Parameter_0_is_not_in_the_same_position_as_parameter_1, source.parameterName, targetPredicate .parameterName);
11840
+ errorReporter!(Diagnostics.Parameter_0_is_not_in_the_same_position_as_parameter_1, source.parameterName, (target as IdentifierTypePredicate) .parameterName);
11856
11841
errorReporter!(Diagnostics.Type_predicate_0_is_not_assignable_to_1, typePredicateToString(source), typePredicateToString(target));
11857
11842
}
11858
11843
return Ternary.False;
@@ -16580,7 +16565,7 @@ namespace ts {
16580
16565
}
16581
16566
16582
16567
if (isIdentifierTypePredicate(predicate)) {
16583
- const predicateArgument = callExpression.arguments[predicate.parameterIndex - (signature.thisParameter ? 1 : 0) ];
16568
+ const predicateArgument = callExpression.arguments[predicate.parameterIndex];
16584
16569
if (predicateArgument) {
16585
16570
if (isMatchingReference(reference, predicateArgument)) {
16586
16571
return getNarrowedType(type, predicate.type, assumeTrue, isTypeSubtypeOf);
@@ -23832,7 +23817,8 @@ namespace ts {
23832
23817
return;
23833
23818
}
23834
23819
23835
- const typePredicate = getTypePredicateOfSignature(getSignatureFromDeclaration(parent));
23820
+ const signature = getSignatureFromDeclaration(parent);
23821
+ const typePredicate = getTypePredicateOfSignature(signature);
23836
23822
if (!typePredicate) {
23837
23823
return;
23838
23824
}
@@ -23845,14 +23831,13 @@ namespace ts {
23845
23831
}
23846
23832
else {
23847
23833
if (typePredicate.parameterIndex >= 0) {
23848
- if (parent.parameters[typePredicate.parameterIndex].dotDotDotToken) {
23849
- error(parameterName,
23850
- Diagnostics.A_type_predicate_cannot_reference_a_rest_parameter);
23834
+ if (signature.hasRestParameter && typePredicate.parameterIndex === signature.parameters.length - 1) {
23835
+ error(parameterName, Diagnostics.A_type_predicate_cannot_reference_a_rest_parameter);
23851
23836
}
23852
23837
else {
23853
23838
const leadingError = () => chainDiagnosticMessages(/*details*/ undefined, Diagnostics.A_type_predicate_s_type_must_be_assignable_to_its_parameter_s_type);
23854
23839
checkTypeAssignableTo(typePredicate.type,
23855
- getTypeOfNode(parent .parameters[typePredicate.parameterIndex]),
23840
+ getTypeOfSymbol(signature .parameters[typePredicate.parameterIndex]),
23856
23841
node.type,
23857
23842
/*headMessage*/ undefined,
23858
23843
leadingError);
0 commit comments