Skip to content

Commit 3dd0814

Browse files
committed
Make IdentifierTypePredicate.parameterIndex index into signature.parameters
1 parent e7afa6c commit 3dd0814

File tree

1 file changed

+12
-27
lines changed

1 file changed

+12
-27
lines changed

src/compiler/checker.ts

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8295,38 +8295,28 @@ namespace ts {
82958295
}
82968296
}
82978297
signature.resolvedTypePredicate = type && isTypePredicateNode(type) ?
8298-
createTypePredicateFromTypePredicateNode(type, signature.declaration!) :
8298+
createTypePredicateFromTypePredicateNode(type, signature) :
82998299
jsdocPredicate || noTypePredicate;
83008300
}
83018301
Debug.assert(!!signature.resolvedTypePredicate);
83028302
}
83038303
return signature.resolvedTypePredicate === noTypePredicate ? undefined : signature.resolvedTypePredicate;
83048304
}
83058305

8306-
function createTypePredicateFromTypePredicateNode(node: TypePredicateNode, func: SignatureDeclaration | JSDocSignature): IdentifierTypePredicate | ThisTypePredicate {
8306+
function createTypePredicateFromTypePredicateNode(node: TypePredicateNode, signature: Signature): IdentifierTypePredicate | ThisTypePredicate {
83078307
const { parameterName } = node;
83088308
const type = getTypeFromTypeNode(node.type);
83098309
if (parameterName.kind === SyntaxKind.Identifier) {
83108310
return createIdentifierTypePredicate(
83118311
parameterName.escapedText as string,
8312-
getTypePredicateParameterIndex(func.parameters, parameterName),
8312+
findIndex(signature.parameters, p => p.escapedName === parameterName.escapedText),
83138313
type);
83148314
}
83158315
else {
83168316
return createThisTypePredicate(type);
83178317
}
83188318
}
83198319

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-
83308320
function getReturnTypeOfSignature(signature: Signature): Type {
83318321
if (!signature.resolvedReturnType) {
83328322
if (!pushTypeResolution(signature, TypeSystemPropertyName.ResolvedReturnType)) {
@@ -11808,7 +11798,7 @@ namespace ts {
1180811798
if (targetTypePredicate) {
1180911799
const sourceTypePredicate = getTypePredicateOfSignature(source);
1181011800
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);
1181211802
}
1181311803
else if (isIdentifierTypePredicate(targetTypePredicate)) {
1181411804
if (reportErrors) {
@@ -11833,8 +11823,6 @@ namespace ts {
1183311823
function compareTypePredicateRelatedTo(
1183411824
source: TypePredicate,
1183511825
target: TypePredicate,
11836-
sourceDeclaration: SignatureDeclaration | JSDocSignature,
11837-
targetDeclaration: SignatureDeclaration | JSDocSignature,
1183811826
reportErrors: boolean,
1183911827
errorReporter: ErrorReporter | undefined,
1184011828
compareTypes: (s: Type, t: Type, reportErrors?: boolean) => Ternary): Ternary {
@@ -11847,12 +11835,9 @@ namespace ts {
1184711835
}
1184811836

1184911837
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) {
1185411839
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);
1185611841
errorReporter!(Diagnostics.Type_predicate_0_is_not_assignable_to_1, typePredicateToString(source), typePredicateToString(target));
1185711842
}
1185811843
return Ternary.False;
@@ -16580,7 +16565,7 @@ namespace ts {
1658016565
}
1658116566

1658216567
if (isIdentifierTypePredicate(predicate)) {
16583-
const predicateArgument = callExpression.arguments[predicate.parameterIndex - (signature.thisParameter ? 1 : 0)];
16568+
const predicateArgument = callExpression.arguments[predicate.parameterIndex];
1658416569
if (predicateArgument) {
1658516570
if (isMatchingReference(reference, predicateArgument)) {
1658616571
return getNarrowedType(type, predicate.type, assumeTrue, isTypeSubtypeOf);
@@ -23832,7 +23817,8 @@ namespace ts {
2383223817
return;
2383323818
}
2383423819

23835-
const typePredicate = getTypePredicateOfSignature(getSignatureFromDeclaration(parent));
23820+
const signature = getSignatureFromDeclaration(parent);
23821+
const typePredicate = getTypePredicateOfSignature(signature);
2383623822
if (!typePredicate) {
2383723823
return;
2383823824
}
@@ -23845,14 +23831,13 @@ namespace ts {
2384523831
}
2384623832
else {
2384723833
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);
2385123836
}
2385223837
else {
2385323838
const leadingError = () => chainDiagnosticMessages(/*details*/ undefined, Diagnostics.A_type_predicate_s_type_must_be_assignable_to_its_parameter_s_type);
2385423839
checkTypeAssignableTo(typePredicate.type,
23855-
getTypeOfNode(parent.parameters[typePredicate.parameterIndex]),
23840+
getTypeOfSymbol(signature.parameters[typePredicate.parameterIndex]),
2385623841
node.type,
2385723842
/*headMessage*/ undefined,
2385823843
leadingError);

0 commit comments

Comments
 (0)