Skip to content

Commit 5651789

Browse files
committed
Fix #29457
Use allTypesAssignableToKind instead of isTypeAssignableToKind to account for union types.
1 parent b2f76e9 commit 5651789

File tree

5 files changed

+87
-17
lines changed

5 files changed

+87
-17
lines changed

src/compiler/checker.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -22153,7 +22153,7 @@ namespace ts {
2215322153
if (!(isTypeComparableTo(leftType, stringType) || isTypeAssignableToKind(leftType, TypeFlags.NumberLike | TypeFlags.ESSymbolLike))) {
2215422154
error(left, Diagnostics.The_left_hand_side_of_an_in_expression_must_be_of_type_any_string_number_or_symbol);
2215522155
}
22156-
if (!isTypeAssignableToKind(rightType, TypeFlags.NonPrimitive | TypeFlags.InstantiableNonPrimitive)) {
22156+
if (!allTypesAssignableToKind(rightType, TypeFlags.NonPrimitive | TypeFlags.InstantiableNonPrimitive)) {
2215722157
error(right, Diagnostics.The_right_hand_side_of_an_in_expression_must_be_of_type_any_an_object_type_or_a_type_parameter);
2215822158
}
2215922159
return booleanType;

tests/baselines/reference/inOperatorWithValidOperands.js

+14
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ function foo<T>(t: T) {
2323
var rb3 = x in t;
2424
}
2525

26+
function unionCase<T, U>(t: T | U) {
27+
var rb4 = x in t;
28+
}
29+
30+
function unionCase2<T>(t: T | object) {
31+
var rb5 = x in t;
32+
}
33+
2634
interface X { x: number }
2735
interface Y { y: number }
2836

@@ -53,6 +61,12 @@ var rb2 = x in {};
5361
function foo(t) {
5462
var rb3 = x in t;
5563
}
64+
function unionCase(t) {
65+
var rb4 = x in t;
66+
}
67+
function unionCase2(t) {
68+
var rb5 = x in t;
69+
}
5670
var c1;
5771
var c2;
5872
var c3;

tests/baselines/reference/inOperatorWithValidOperands.symbols

+42-16
Original file line numberDiff line numberDiff line change
@@ -59,35 +59,61 @@ function foo<T>(t: T) {
5959
>t : Symbol(t, Decl(inOperatorWithValidOperands.ts, 20, 16))
6060
}
6161

62+
function unionCase<T, U>(t: T | U) {
63+
>unionCase : Symbol(unionCase, Decl(inOperatorWithValidOperands.ts, 22, 1))
64+
>T : Symbol(T, Decl(inOperatorWithValidOperands.ts, 24, 19))
65+
>U : Symbol(U, Decl(inOperatorWithValidOperands.ts, 24, 21))
66+
>t : Symbol(t, Decl(inOperatorWithValidOperands.ts, 24, 25))
67+
>T : Symbol(T, Decl(inOperatorWithValidOperands.ts, 24, 19))
68+
>U : Symbol(U, Decl(inOperatorWithValidOperands.ts, 24, 21))
69+
70+
var rb4 = x in t;
71+
>rb4 : Symbol(rb4, Decl(inOperatorWithValidOperands.ts, 25, 7))
72+
>x : Symbol(x, Decl(inOperatorWithValidOperands.ts, 0, 3))
73+
>t : Symbol(t, Decl(inOperatorWithValidOperands.ts, 24, 25))
74+
}
75+
76+
function unionCase2<T>(t: T | object) {
77+
>unionCase2 : Symbol(unionCase2, Decl(inOperatorWithValidOperands.ts, 26, 1))
78+
>T : Symbol(T, Decl(inOperatorWithValidOperands.ts, 28, 20))
79+
>t : Symbol(t, Decl(inOperatorWithValidOperands.ts, 28, 23))
80+
>T : Symbol(T, Decl(inOperatorWithValidOperands.ts, 28, 20))
81+
82+
var rb5 = x in t;
83+
>rb5 : Symbol(rb5, Decl(inOperatorWithValidOperands.ts, 29, 7))
84+
>x : Symbol(x, Decl(inOperatorWithValidOperands.ts, 0, 3))
85+
>t : Symbol(t, Decl(inOperatorWithValidOperands.ts, 28, 23))
86+
}
87+
6288
interface X { x: number }
63-
>X : Symbol(X, Decl(inOperatorWithValidOperands.ts, 22, 1))
64-
>x : Symbol(X.x, Decl(inOperatorWithValidOperands.ts, 24, 13))
89+
>X : Symbol(X, Decl(inOperatorWithValidOperands.ts, 30, 1))
90+
>x : Symbol(X.x, Decl(inOperatorWithValidOperands.ts, 32, 13))
6591

6692
interface Y { y: number }
67-
>Y : Symbol(Y, Decl(inOperatorWithValidOperands.ts, 24, 25))
68-
>y : Symbol(Y.y, Decl(inOperatorWithValidOperands.ts, 25, 13))
93+
>Y : Symbol(Y, Decl(inOperatorWithValidOperands.ts, 32, 25))
94+
>y : Symbol(Y.y, Decl(inOperatorWithValidOperands.ts, 33, 13))
6995

7096
var c1: X | Y;
71-
>c1 : Symbol(c1, Decl(inOperatorWithValidOperands.ts, 27, 3))
72-
>X : Symbol(X, Decl(inOperatorWithValidOperands.ts, 22, 1))
73-
>Y : Symbol(Y, Decl(inOperatorWithValidOperands.ts, 24, 25))
97+
>c1 : Symbol(c1, Decl(inOperatorWithValidOperands.ts, 35, 3))
98+
>X : Symbol(X, Decl(inOperatorWithValidOperands.ts, 30, 1))
99+
>Y : Symbol(Y, Decl(inOperatorWithValidOperands.ts, 32, 25))
74100

75101
var c2: X;
76-
>c2 : Symbol(c2, Decl(inOperatorWithValidOperands.ts, 28, 3))
77-
>X : Symbol(X, Decl(inOperatorWithValidOperands.ts, 22, 1))
102+
>c2 : Symbol(c2, Decl(inOperatorWithValidOperands.ts, 36, 3))
103+
>X : Symbol(X, Decl(inOperatorWithValidOperands.ts, 30, 1))
78104

79105
var c3: Y;
80-
>c3 : Symbol(c3, Decl(inOperatorWithValidOperands.ts, 29, 3))
81-
>Y : Symbol(Y, Decl(inOperatorWithValidOperands.ts, 24, 25))
106+
>c3 : Symbol(c3, Decl(inOperatorWithValidOperands.ts, 37, 3))
107+
>Y : Symbol(Y, Decl(inOperatorWithValidOperands.ts, 32, 25))
82108

83109
var rc1 = x in c1;
84-
>rc1 : Symbol(rc1, Decl(inOperatorWithValidOperands.ts, 31, 3))
110+
>rc1 : Symbol(rc1, Decl(inOperatorWithValidOperands.ts, 39, 3))
85111
>x : Symbol(x, Decl(inOperatorWithValidOperands.ts, 0, 3))
86-
>c1 : Symbol(c1, Decl(inOperatorWithValidOperands.ts, 27, 3))
112+
>c1 : Symbol(c1, Decl(inOperatorWithValidOperands.ts, 35, 3))
87113

88114
var rc2 = x in (c2 || c3);
89-
>rc2 : Symbol(rc2, Decl(inOperatorWithValidOperands.ts, 32, 3))
115+
>rc2 : Symbol(rc2, Decl(inOperatorWithValidOperands.ts, 40, 3))
90116
>x : Symbol(x, Decl(inOperatorWithValidOperands.ts, 0, 3))
91-
>c2 : Symbol(c2, Decl(inOperatorWithValidOperands.ts, 28, 3))
92-
>c3 : Symbol(c3, Decl(inOperatorWithValidOperands.ts, 29, 3))
117+
>c2 : Symbol(c2, Decl(inOperatorWithValidOperands.ts, 36, 3))
118+
>c3 : Symbol(c3, Decl(inOperatorWithValidOperands.ts, 37, 3))
93119

tests/baselines/reference/inOperatorWithValidOperands.types

+22
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,28 @@ function foo<T>(t: T) {
6868
>t : T
6969
}
7070

71+
function unionCase<T, U>(t: T | U) {
72+
>unionCase : <T, U>(t: T | U) => void
73+
>t : T | U
74+
75+
var rb4 = x in t;
76+
>rb4 : boolean
77+
>x in t : boolean
78+
>x : any
79+
>t : T | U
80+
}
81+
82+
function unionCase2<T>(t: T | object) {
83+
>unionCase2 : <T>(t: object | T) => void
84+
>t : object | T
85+
86+
var rb5 = x in t;
87+
>rb5 : boolean
88+
>x in t : boolean
89+
>x : any
90+
>t : object | T
91+
}
92+
7193
interface X { x: number }
7294
>x : number
7395

tests/cases/conformance/expressions/binaryOperators/inOperator/inOperatorWithValidOperands.ts

+8
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ function foo<T>(t: T) {
2222
var rb3 = x in t;
2323
}
2424

25+
function unionCase<T, U>(t: T | U) {
26+
var rb4 = x in t;
27+
}
28+
29+
function unionCase2<T>(t: T | object) {
30+
var rb5 = x in t;
31+
}
32+
2533
interface X { x: number }
2634
interface Y { y: number }
2735

0 commit comments

Comments
 (0)