Skip to content

Commit 649f5c0

Browse files
committed
do not report cascading errors in instanceof operator
1 parent 8be8e1f commit 649f5c0

File tree

5 files changed

+40
-9
lines changed

5 files changed

+40
-9
lines changed

src/compiler/checker.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4935,10 +4935,12 @@ module ts {
49354935
// The instanceof operator requires the left operand to be of type Any, an object type, or a type parameter type,
49364936
// and the right operand to be of type Any or a subtype of the 'Function' interface type.
49374937
// The result is always of the Boolean primitive type.
4938-
if (!isTypeAnyTypeObjectTypeOrTypeParameter(leftType)) {
4938+
// NOTE: do not raise error is leftType is unknown as related error was already reported
4939+
if (leftType !== unknownType && !isTypeAnyTypeObjectTypeOrTypeParameter(leftType)) {
49394940
error(node.left, Diagnostics.The_left_hand_side_of_an_instanceof_expression_must_be_of_type_any_an_object_type_or_a_type_parameter);
49404941
}
4941-
if (rightType !== anyType && !isTypeSubtypeOf(rightType, globalFunctionType)) {
4942+
// NOTE: do not raise error is right is unknown as related error was already reported
4943+
if (rightType !== unknownType && rightType !== anyType && !isTypeSubtypeOf(rightType, globalFunctionType)) {
49424944
error(node.right, Diagnostics.The_right_hand_side_of_an_instanceof_expression_must_be_of_type_any_or_of_a_type_assignable_to_the_Function_interface_type);
49434945
}
49444946
return booleanType;

tests/baselines/reference/constructorWithIncompleteTypeAnnotation.errors.txt

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,6 @@ tests/cases/compiler/constructorWithIncompleteTypeAnnotation.ts(40,28): error TS
6868
tests/cases/compiler/constructorWithIncompleteTypeAnnotation.ts(41,21): error TS2304: Cannot find name 'retValue'.
6969
tests/cases/compiler/constructorWithIncompleteTypeAnnotation.ts(47,17): error TS2304: Cannot find name 'console'.
7070
tests/cases/compiler/constructorWithIncompleteTypeAnnotation.ts(53,13): error TS2304: Cannot find name 'console'.
71-
tests/cases/compiler/constructorWithIncompleteTypeAnnotation.ts(76,26): error TS2358: The left-hand side of an 'instanceof' expression must be of type 'any', an object type or a type parameter.
72-
tests/cases/compiler/constructorWithIncompleteTypeAnnotation.ts(76,44): error TS2359: The right-hand side of an 'instanceof' expression must be of type 'any' or of a type assignable to the 'Function' interface type.
7371
tests/cases/compiler/constructorWithIncompleteTypeAnnotation.ts(89,23): error TS2364: Invalid left-hand side of assignment expression.
7472
tests/cases/compiler/constructorWithIncompleteTypeAnnotation.ts(108,24): error TS2365: Operator '+' cannot be applied to types 'number' and 'boolean'.
7573
tests/cases/compiler/constructorWithIncompleteTypeAnnotation.ts(159,31): error TS2304: Cannot find name 'Property'.
@@ -98,7 +96,7 @@ tests/cases/compiler/constructorWithIncompleteTypeAnnotation.ts(259,29): error T
9896
tests/cases/compiler/constructorWithIncompleteTypeAnnotation.ts(259,37): error TS2304: Cannot find name 'string'.
9997

10098

101-
==== tests/cases/compiler/constructorWithIncompleteTypeAnnotation.ts (98 errors) ====
99+
==== tests/cases/compiler/constructorWithIncompleteTypeAnnotation.ts (96 errors) ====
102100
declare module "fs" {
103101
export class File {
104102
constructor(filename: string);
@@ -242,10 +240,6 @@ tests/cases/compiler/constructorWithIncompleteTypeAnnotation.ts(259,37): error T
242240

243241
var local5 = <fs.File>null;
244242
var local6 = local5 instanceof fs.File;
245-
~~~~~~
246-
!!! error TS2358: The left-hand side of an 'instanceof' expression must be of type 'any', an object type or a type parameter.
247-
~~~~~~~
248-
!!! error TS2359: The right-hand side of an 'instanceof' expression must be of type 'any' or of a type assignable to the 'Function' interface type.
249243

250244
var hex = 0xBADC0DE, Hex = 0XDEADBEEF;
251245
var float = 6.02e23, float2 = 6.02E-23
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
tests/cases/compiler/errorHandlingInInstanceOf.ts(1,5): error TS2304: Cannot find name 'x'.
2+
tests/cases/compiler/errorHandlingInInstanceOf.ts(5,18): error TS2304: Cannot find name 'UnknownType'.
3+
4+
5+
==== tests/cases/compiler/errorHandlingInInstanceOf.ts (2 errors) ====
6+
if (x instanceof String) {
7+
~
8+
!!! error TS2304: Cannot find name 'x'.
9+
}
10+
11+
var y: any;
12+
if (y instanceof UnknownType) {
13+
~~~~~~~~~~~
14+
!!! error TS2304: Cannot find name 'UnknownType'.
15+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//// [errorHandlingInInstanceOf.ts]
2+
if (x instanceof String) {
3+
}
4+
5+
var y: any;
6+
if (y instanceof UnknownType) {
7+
}
8+
9+
//// [errorHandlingInInstanceOf.js]
10+
if (x instanceof String) {
11+
}
12+
var y;
13+
if (y instanceof UnknownType) {
14+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
if (x instanceof String) {
2+
}
3+
4+
var y: any;
5+
if (y instanceof UnknownType) {
6+
}

0 commit comments

Comments
 (0)