Skip to content

Commit 0459987

Browse files
Merge pull request #14467 from kpreisser/fix14269
Allow the right-hand side of an 'in' expression to be of non-primitive object type
2 parents 2e313ca + 6d02d1f commit 0459987

File tree

5 files changed

+69
-1
lines changed

5 files changed

+69
-1
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16306,7 +16306,7 @@ namespace ts {
1630616306
if (!(isTypeComparableTo(leftType, stringType) || isTypeOfKind(leftType, TypeFlags.NumberLike | TypeFlags.ESSymbol))) {
1630716307
error(left, Diagnostics.The_left_hand_side_of_an_in_expression_must_be_of_type_any_string_number_or_symbol);
1630816308
}
16309-
if (!isTypeAnyOrAllConstituentTypesHaveKind(rightType, TypeFlags.Object | TypeFlags.TypeVariable)) {
16309+
if (!isTypeAnyOrAllConstituentTypesHaveKind(rightType, TypeFlags.Object | TypeFlags.TypeVariable | TypeFlags.NonPrimitive)) {
1631016310
error(right, Diagnostics.The_right_hand_side_of_an_in_expression_must_be_of_type_any_an_object_type_or_a_type_parameter);
1631116311
}
1631216312
return booleanType;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//// [nonPrimitiveRhsSideOfInExpression.ts]
2+
let o: object = {};
3+
4+
function f(): object {
5+
return {};
6+
}
7+
8+
const b1 = "foo" in o;
9+
const b2 = "bar" in f();
10+
11+
//// [nonPrimitiveRhsSideOfInExpression.js]
12+
var o = {};
13+
function f() {
14+
return {};
15+
}
16+
var b1 = "foo" in o;
17+
var b2 = "bar" in f();
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
=== tests/cases/conformance/types/nonPrimitive/nonPrimitiveRhsSideOfInExpression.ts ===
2+
let o: object = {};
3+
>o : Symbol(o, Decl(nonPrimitiveRhsSideOfInExpression.ts, 0, 3))
4+
5+
function f(): object {
6+
>f : Symbol(f, Decl(nonPrimitiveRhsSideOfInExpression.ts, 0, 19))
7+
8+
return {};
9+
}
10+
11+
const b1 = "foo" in o;
12+
>b1 : Symbol(b1, Decl(nonPrimitiveRhsSideOfInExpression.ts, 6, 5))
13+
>o : Symbol(o, Decl(nonPrimitiveRhsSideOfInExpression.ts, 0, 3))
14+
15+
const b2 = "bar" in f();
16+
>b2 : Symbol(b2, Decl(nonPrimitiveRhsSideOfInExpression.ts, 7, 5))
17+
>f : Symbol(f, Decl(nonPrimitiveRhsSideOfInExpression.ts, 0, 19))
18+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
=== tests/cases/conformance/types/nonPrimitive/nonPrimitiveRhsSideOfInExpression.ts ===
2+
let o: object = {};
3+
>o : object
4+
>{} : {}
5+
6+
function f(): object {
7+
>f : () => object
8+
9+
return {};
10+
>{} : {}
11+
}
12+
13+
const b1 = "foo" in o;
14+
>b1 : boolean
15+
>"foo" in o : boolean
16+
>"foo" : "foo"
17+
>o : object
18+
19+
const b2 = "bar" in f();
20+
>b2 : boolean
21+
>"bar" in f() : boolean
22+
>"bar" : "bar"
23+
>f() : object
24+
>f : () => object
25+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
let o: object = {};
2+
3+
function f(): object {
4+
return {};
5+
}
6+
7+
const b1 = "foo" in o;
8+
const b2 = "bar" in f();

0 commit comments

Comments
 (0)