Skip to content

Commit 8da7db9

Browse files
committed
Fixes CR feedback
1 parent fa9a914 commit 8da7db9

File tree

4 files changed

+34
-13
lines changed

4 files changed

+34
-13
lines changed

src/compiler/checker.ts

+23-12
Original file line numberDiff line numberDiff line change
@@ -5642,13 +5642,13 @@ module ts {
56425642
}
56435643

56445644
if (targetType) {
5645-
return getOptionalNarrowedType(type, targetType);
5645+
return getNarrowedType(type, targetType);
56465646
}
56475647

56485648
return type;
56495649
}
56505650

5651-
function getOptionalNarrowedType(originalType: Type, narrowedTypeCandidate: Type) {
5651+
function getNarrowedType(originalType: Type, narrowedTypeCandidate: Type) {
56525652
// Narrow to the target type if it's a subtype of the current type
56535653
if (isTypeSubtypeOf(narrowedTypeCandidate, originalType)) {
56545654
return narrowedTypeCandidate;
@@ -5675,7 +5675,7 @@ module ts {
56755675
}
56765676
return type;
56775677
}
5678-
return getOptionalNarrowedType(type, signature.typePredicate.type);
5678+
return getNarrowedType(type, signature.typePredicate.type);
56795679
}
56805680
return type;
56815681
}
@@ -8627,9 +8627,15 @@ module ts {
86278627
}
86288628
else {
86298629
if (typePredicate.parameterIndex >= 0) {
8630-
checkTypeAssignableTo(typePredicate.type,
8631-
getTypeAtLocation(node.parameters[typePredicate.parameterIndex]),
8632-
typePredicateNode.type);
8630+
if (node.parameters[typePredicate.parameterIndex].dotDotDotToken) {
8631+
error(typePredicateNode.parameterName,
8632+
Diagnostics.Type_predicate_cannot_reference_a_spread_parameter);
8633+
}
8634+
else {
8635+
checkTypeAssignableTo(typePredicate.type,
8636+
getTypeAtLocation(node.parameters[typePredicate.parameterIndex]),
8637+
typePredicateNode.type);
8638+
}
86338639
}
86348640
else if (typePredicateNode.parameterName) {
86358641
error(typePredicateNode.parameterName,
@@ -11275,6 +11281,16 @@ module ts {
1127511281
}
1127611282
}
1127711283

11284+
function checkTypePredicate(node: TypePredicateNode) {
11285+
switch (node.parent.kind) {
11286+
case SyntaxKind.ArrowFunction:
11287+
case SyntaxKind.FunctionDeclaration:
11288+
case SyntaxKind.FunctionExpression:
11289+
return;
11290+
}
11291+
error(node, Diagnostics.Type_predicates_are_only_allowed_in_return_type_position);
11292+
}
11293+
1127811294
function checkSourceElement(node: Node): void {
1127911295
if (!node) return;
1128011296
switch (node.kind) {
@@ -11303,12 +11319,7 @@ module ts {
1130311319
case SyntaxKind.TypeReference:
1130411320
return checkTypeReferenceNode(<TypeReferenceNode>node);
1130511321
case SyntaxKind.TypePredicate:
11306-
// Issue an error every time we encounter a type predicate. They are only allowed
11307-
// in return type positions in signature declarations. checkSignatureDeclaration(..)
11308-
// already have a specific check for type predicates, so every time we encounter a type
11309-
// predicate in checkSourceElement it must be in a non return type position.
11310-
error(node, Diagnostics.Type_predicates_are_only_allowed_in_return_type_position);
11311-
return;
11322+
return checkTypePredicate(<TypePredicateNode>node);
1131211323
case SyntaxKind.TypeQuery:
1131311324
return checkTypeQuery(<TypeQueryNode>node);
1131411325
case SyntaxKind.TypeLiteral:

src/compiler/diagnosticInformationMap.generated.ts

+1
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ module ts {
184184
Type_predicate_0_is_not_assignable_to_1: { code: 1226, category: DiagnosticCategory.Error, key: "Type predicate '{0}' is not assignable to '{1}'." },
185185
Parameter_0_is_not_in_the_same_position_as_parameter_1: { code: 1227, category: DiagnosticCategory.Error, key: "Parameter '{0}' is not in the same position as parameter '{1}'." },
186186
Type_predicates_are_only_allowed_in_return_type_position: { code: 1228, category: DiagnosticCategory.Error, key: "Type predicates are only allowed in return type position." },
187+
Type_predicate_cannot_reference_a_spread_parameter: { code: 1229, category: DiagnosticCategory.Error, key: "Type predicate cannot reference a spread parameter." },
187188
Duplicate_identifier_0: { code: 2300, category: DiagnosticCategory.Error, key: "Duplicate identifier '{0}'." },
188189
Initializer_of_instance_member_variable_0_cannot_reference_identifier_1_declared_in_the_constructor: { code: 2301, category: DiagnosticCategory.Error, key: "Initializer of instance member variable '{0}' cannot reference identifier '{1}' declared in the constructor." },
189190
Static_members_cannot_reference_class_type_parameters: { code: 2302, category: DiagnosticCategory.Error, key: "Static members cannot reference class type parameters." },

src/compiler/diagnosticMessages.json

+4
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,10 @@
723723
"category": "Error",
724724
"code": 1228
725725
},
726+
"Type predicate cannot reference a spread parameter.": {
727+
"category": "Error",
728+
"code": 1229
729+
},
726730

727731

728732
"Duplicate identifier '{0}'.": {

tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,9 @@ var b1: b is A;
9696
function b2(a: b is A) {};
9797
function b3(): A | b is A {
9898
return true;
99-
};
99+
};
100+
101+
// Reference to spread parameter
102+
function b4(...a): a is A {
103+
return true;
104+
}

0 commit comments

Comments
 (0)