Use allTypesAssignableToKind instead of isTypeAssignableToKind to
account for union types.
This commit is contained in:
Jack Williams 2019-01-17 20:03:46 +00:00
parent b2f76e9dc8
commit 5651789629
5 changed files with 87 additions and 17 deletions

View file

@ -22153,7 +22153,7 @@ namespace ts {
if (!(isTypeComparableTo(leftType, stringType) || isTypeAssignableToKind(leftType, TypeFlags.NumberLike | TypeFlags.ESSymbolLike))) {
error(left, Diagnostics.The_left_hand_side_of_an_in_expression_must_be_of_type_any_string_number_or_symbol);
}
if (!isTypeAssignableToKind(rightType, TypeFlags.NonPrimitive | TypeFlags.InstantiableNonPrimitive)) {
if (!allTypesAssignableToKind(rightType, TypeFlags.NonPrimitive | TypeFlags.InstantiableNonPrimitive)) {
error(right, Diagnostics.The_right_hand_side_of_an_in_expression_must_be_of_type_any_an_object_type_or_a_type_parameter);
}
return booleanType;

View file

@ -23,6 +23,14 @@ function foo<T>(t: T) {
var rb3 = x in t;
}
function unionCase<T, U>(t: T | U) {
var rb4 = x in t;
}
function unionCase2<T>(t: T | object) {
var rb5 = x in t;
}
interface X { x: number }
interface Y { y: number }
@ -53,6 +61,12 @@ var rb2 = x in {};
function foo(t) {
var rb3 = x in t;
}
function unionCase(t) {
var rb4 = x in t;
}
function unionCase2(t) {
var rb5 = x in t;
}
var c1;
var c2;
var c3;

View file

@ -59,35 +59,61 @@ function foo<T>(t: T) {
>t : Symbol(t, Decl(inOperatorWithValidOperands.ts, 20, 16))
}
function unionCase<T, U>(t: T | U) {
>unionCase : Symbol(unionCase, Decl(inOperatorWithValidOperands.ts, 22, 1))
>T : Symbol(T, Decl(inOperatorWithValidOperands.ts, 24, 19))
>U : Symbol(U, Decl(inOperatorWithValidOperands.ts, 24, 21))
>t : Symbol(t, Decl(inOperatorWithValidOperands.ts, 24, 25))
>T : Symbol(T, Decl(inOperatorWithValidOperands.ts, 24, 19))
>U : Symbol(U, Decl(inOperatorWithValidOperands.ts, 24, 21))
var rb4 = x in t;
>rb4 : Symbol(rb4, Decl(inOperatorWithValidOperands.ts, 25, 7))
>x : Symbol(x, Decl(inOperatorWithValidOperands.ts, 0, 3))
>t : Symbol(t, Decl(inOperatorWithValidOperands.ts, 24, 25))
}
function unionCase2<T>(t: T | object) {
>unionCase2 : Symbol(unionCase2, Decl(inOperatorWithValidOperands.ts, 26, 1))
>T : Symbol(T, Decl(inOperatorWithValidOperands.ts, 28, 20))
>t : Symbol(t, Decl(inOperatorWithValidOperands.ts, 28, 23))
>T : Symbol(T, Decl(inOperatorWithValidOperands.ts, 28, 20))
var rb5 = x in t;
>rb5 : Symbol(rb5, Decl(inOperatorWithValidOperands.ts, 29, 7))
>x : Symbol(x, Decl(inOperatorWithValidOperands.ts, 0, 3))
>t : Symbol(t, Decl(inOperatorWithValidOperands.ts, 28, 23))
}
interface X { x: number }
>X : Symbol(X, Decl(inOperatorWithValidOperands.ts, 22, 1))
>x : Symbol(X.x, Decl(inOperatorWithValidOperands.ts, 24, 13))
>X : Symbol(X, Decl(inOperatorWithValidOperands.ts, 30, 1))
>x : Symbol(X.x, Decl(inOperatorWithValidOperands.ts, 32, 13))
interface Y { y: number }
>Y : Symbol(Y, Decl(inOperatorWithValidOperands.ts, 24, 25))
>y : Symbol(Y.y, Decl(inOperatorWithValidOperands.ts, 25, 13))
>Y : Symbol(Y, Decl(inOperatorWithValidOperands.ts, 32, 25))
>y : Symbol(Y.y, Decl(inOperatorWithValidOperands.ts, 33, 13))
var c1: X | Y;
>c1 : Symbol(c1, Decl(inOperatorWithValidOperands.ts, 27, 3))
>X : Symbol(X, Decl(inOperatorWithValidOperands.ts, 22, 1))
>Y : Symbol(Y, Decl(inOperatorWithValidOperands.ts, 24, 25))
>c1 : Symbol(c1, Decl(inOperatorWithValidOperands.ts, 35, 3))
>X : Symbol(X, Decl(inOperatorWithValidOperands.ts, 30, 1))
>Y : Symbol(Y, Decl(inOperatorWithValidOperands.ts, 32, 25))
var c2: X;
>c2 : Symbol(c2, Decl(inOperatorWithValidOperands.ts, 28, 3))
>X : Symbol(X, Decl(inOperatorWithValidOperands.ts, 22, 1))
>c2 : Symbol(c2, Decl(inOperatorWithValidOperands.ts, 36, 3))
>X : Symbol(X, Decl(inOperatorWithValidOperands.ts, 30, 1))
var c3: Y;
>c3 : Symbol(c3, Decl(inOperatorWithValidOperands.ts, 29, 3))
>Y : Symbol(Y, Decl(inOperatorWithValidOperands.ts, 24, 25))
>c3 : Symbol(c3, Decl(inOperatorWithValidOperands.ts, 37, 3))
>Y : Symbol(Y, Decl(inOperatorWithValidOperands.ts, 32, 25))
var rc1 = x in c1;
>rc1 : Symbol(rc1, Decl(inOperatorWithValidOperands.ts, 31, 3))
>rc1 : Symbol(rc1, Decl(inOperatorWithValidOperands.ts, 39, 3))
>x : Symbol(x, Decl(inOperatorWithValidOperands.ts, 0, 3))
>c1 : Symbol(c1, Decl(inOperatorWithValidOperands.ts, 27, 3))
>c1 : Symbol(c1, Decl(inOperatorWithValidOperands.ts, 35, 3))
var rc2 = x in (c2 || c3);
>rc2 : Symbol(rc2, Decl(inOperatorWithValidOperands.ts, 32, 3))
>rc2 : Symbol(rc2, Decl(inOperatorWithValidOperands.ts, 40, 3))
>x : Symbol(x, Decl(inOperatorWithValidOperands.ts, 0, 3))
>c2 : Symbol(c2, Decl(inOperatorWithValidOperands.ts, 28, 3))
>c3 : Symbol(c3, Decl(inOperatorWithValidOperands.ts, 29, 3))
>c2 : Symbol(c2, Decl(inOperatorWithValidOperands.ts, 36, 3))
>c3 : Symbol(c3, Decl(inOperatorWithValidOperands.ts, 37, 3))

View file

@ -68,6 +68,28 @@ function foo<T>(t: T) {
>t : T
}
function unionCase<T, U>(t: T | U) {
>unionCase : <T, U>(t: T | U) => void
>t : T | U
var rb4 = x in t;
>rb4 : boolean
>x in t : boolean
>x : any
>t : T | U
}
function unionCase2<T>(t: T | object) {
>unionCase2 : <T>(t: object | T) => void
>t : object | T
var rb5 = x in t;
>rb5 : boolean
>x in t : boolean
>x : any
>t : object | T
}
interface X { x: number }
>x : number

View file

@ -22,6 +22,14 @@ function foo<T>(t: T) {
var rb3 = x in t;
}
function unionCase<T, U>(t: T | U) {
var rb4 = x in t;
}
function unionCase2<T>(t: T | object) {
var rb5 = x in t;
}
interface X { x: number }
interface Y { y: number }