Skip to content

Commit d2cd9b1

Browse files
committed
review
1 parent 8c7ca65 commit d2cd9b1

9 files changed

Lines changed: 98 additions & 919 deletions

src/compiler/checker.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29996,13 +29996,14 @@ namespace ts {
2999629996
rightType = checkNonNullType(rightType, right);
2999729997
// TypeScript 1.0 spec (April 2014): 4.15.5
2999829998
// The in operator requires the left operand to be of type Any, the String primitive type, or the Number primitive type,
29999-
// and the right operand to be of type Any, an object type, or a type parameter type.
29999+
// and the right operand not to extend a primitive type.
3000030000
// The result is always of the Boolean primitive type.
3000130001
if (!(allTypesAssignableToKind(leftType, TypeFlags.StringLike | TypeFlags.NumberLike | TypeFlags.ESSymbolLike) ||
3000230002
isTypeAssignableToKind(leftType, TypeFlags.Index | TypeFlags.TemplateLiteral | TypeFlags.StringMapping | TypeFlags.TypeParameter))) {
3000330003
error(left, Diagnostics.The_left_hand_side_of_an_in_expression_must_be_of_type_any_string_number_or_symbol);
3000430004
}
30005-
if (!isTypeAssignableToKind(rightType, TypeFlags.NonPrimitive)) {
30005+
if (!allTypesAssignableToKind(rightType, TypeFlags.NonPrimitive | TypeFlags.InstantiableNonPrimitive) ||
30006+
rightType.immediateBaseConstraint && allTypesAssignableToKind(rightType.immediateBaseConstraint, TypeFlags.Primitive)) {
3000630007
error(right, Diagnostics.The_right_hand_side_of_an_in_expression_must_be_of_type_any_an_object_type_or_a_type_parameter);
3000730008
}
3000830009
return booleanType;

tests/baselines/reference/conditionalTypeDoesntSpinForever.errors.txt

Lines changed: 0 additions & 154 deletions
This file was deleted.
Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,27 @@
1-
tests/cases/compiler/inDoesNotOperateOnPrimitiveTypes.ts(12,17): error TS2361: The right-hand side of an 'in' expression must be of type 'any', an object type or a type parameter.
1+
tests/cases/compiler/inDoesNotOperateOnPrimitiveTypes.ts(19,17): error TS2361: The right-hand side of an 'in' expression must be of type 'any', an object type or a type parameter.
22

33

44
==== tests/cases/compiler/inDoesNotOperateOnPrimitiveTypes.ts (1 errors) ====
5-
const validHasKey = <A extends object>(
6-
thing: A,
5+
const validHasKey = <T extends object>(
6+
thing: T,
77
key: string,
88
): boolean => {
99
return key in thing;
1010
};
1111

12-
const invalidHasKey = <A>(
13-
thing: A,
12+
const alsoValidHasKey = <T>(
13+
thing: T,
1414
key: string,
1515
): boolean => {
16+
return key in thing;
17+
};
18+
19+
function invalidHasKey<T extends string | number>(
20+
thing: T,
21+
key: string,
22+
): boolean {
1623
return key in thing;
1724
~~~~~
1825
!!! error TS2361: The right-hand side of an 'in' expression must be of type 'any', an object type or a type parameter.
19-
};
26+
}
2027

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,33 @@
11
//// [inDoesNotOperateOnPrimitiveTypes.ts]
2-
const validHasKey = <A extends object>(
3-
thing: A,
2+
const validHasKey = <T extends object>(
3+
thing: T,
44
key: string,
55
): boolean => {
66
return key in thing;
77
};
88

9-
const invalidHasKey = <A>(
10-
thing: A,
9+
const alsoValidHasKey = <T>(
10+
thing: T,
1111
key: string,
1212
): boolean => {
1313
return key in thing;
1414
};
15+
16+
function invalidHasKey<T extends string | number>(
17+
thing: T,
18+
key: string,
19+
): boolean {
20+
return key in thing;
21+
}
1522

1623

1724
//// [inDoesNotOperateOnPrimitiveTypes.js]
1825
var validHasKey = function (thing, key) {
1926
return key in thing;
2027
};
21-
var invalidHasKey = function (thing, key) {
28+
var alsoValidHasKey = function (thing, key) {
2229
return key in thing;
2330
};
31+
function invalidHasKey(thing, key) {
32+
return key in thing;
33+
}
Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
=== tests/cases/compiler/inDoesNotOperateOnPrimitiveTypes.ts ===
2-
const validHasKey = <A extends object>(
2+
const validHasKey = <T extends object>(
33
>validHasKey : Symbol(validHasKey, Decl(inDoesNotOperateOnPrimitiveTypes.ts, 0, 5))
4-
>A : Symbol(A, Decl(inDoesNotOperateOnPrimitiveTypes.ts, 0, 21))
4+
>T : Symbol(T, Decl(inDoesNotOperateOnPrimitiveTypes.ts, 0, 21))
55

6-
thing: A,
6+
thing: T,
77
>thing : Symbol(thing, Decl(inDoesNotOperateOnPrimitiveTypes.ts, 0, 39))
8-
>A : Symbol(A, Decl(inDoesNotOperateOnPrimitiveTypes.ts, 0, 21))
8+
>T : Symbol(T, Decl(inDoesNotOperateOnPrimitiveTypes.ts, 0, 21))
99

1010
key: string,
1111
>key : Symbol(key, Decl(inDoesNotOperateOnPrimitiveTypes.ts, 1, 11))
@@ -17,21 +17,38 @@ const validHasKey = <A extends object>(
1717

1818
};
1919

20-
const invalidHasKey = <A>(
21-
>invalidHasKey : Symbol(invalidHasKey, Decl(inDoesNotOperateOnPrimitiveTypes.ts, 7, 5))
22-
>A : Symbol(A, Decl(inDoesNotOperateOnPrimitiveTypes.ts, 7, 23))
20+
const alsoValidHasKey = <T>(
21+
>alsoValidHasKey : Symbol(alsoValidHasKey, Decl(inDoesNotOperateOnPrimitiveTypes.ts, 7, 5))
22+
>T : Symbol(T, Decl(inDoesNotOperateOnPrimitiveTypes.ts, 7, 25))
2323

24-
thing: A,
25-
>thing : Symbol(thing, Decl(inDoesNotOperateOnPrimitiveTypes.ts, 7, 26))
26-
>A : Symbol(A, Decl(inDoesNotOperateOnPrimitiveTypes.ts, 7, 23))
24+
thing: T,
25+
>thing : Symbol(thing, Decl(inDoesNotOperateOnPrimitiveTypes.ts, 7, 28))
26+
>T : Symbol(T, Decl(inDoesNotOperateOnPrimitiveTypes.ts, 7, 25))
2727

2828
key: string,
2929
>key : Symbol(key, Decl(inDoesNotOperateOnPrimitiveTypes.ts, 8, 11))
3030

3131
): boolean => {
3232
return key in thing;
3333
>key : Symbol(key, Decl(inDoesNotOperateOnPrimitiveTypes.ts, 8, 11))
34-
>thing : Symbol(thing, Decl(inDoesNotOperateOnPrimitiveTypes.ts, 7, 26))
34+
>thing : Symbol(thing, Decl(inDoesNotOperateOnPrimitiveTypes.ts, 7, 28))
3535

3636
};
3737

38+
function invalidHasKey<T extends string | number>(
39+
>invalidHasKey : Symbol(invalidHasKey, Decl(inDoesNotOperateOnPrimitiveTypes.ts, 12, 2))
40+
>T : Symbol(T, Decl(inDoesNotOperateOnPrimitiveTypes.ts, 14, 23))
41+
42+
thing: T,
43+
>thing : Symbol(thing, Decl(inDoesNotOperateOnPrimitiveTypes.ts, 14, 50))
44+
>T : Symbol(T, Decl(inDoesNotOperateOnPrimitiveTypes.ts, 14, 23))
45+
46+
key: string,
47+
>key : Symbol(key, Decl(inDoesNotOperateOnPrimitiveTypes.ts, 15, 11))
48+
49+
): boolean {
50+
return key in thing;
51+
>key : Symbol(key, Decl(inDoesNotOperateOnPrimitiveTypes.ts, 15, 11))
52+
>thing : Symbol(thing, Decl(inDoesNotOperateOnPrimitiveTypes.ts, 14, 50))
53+
}
54+
Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
=== tests/cases/compiler/inDoesNotOperateOnPrimitiveTypes.ts ===
2-
const validHasKey = <A extends object>(
3-
>validHasKey : <A extends object>(thing: A, key: string) => boolean
4-
><A extends object>( thing: A, key: string,): boolean => { return key in thing;} : <A extends object>(thing: A, key: string) => boolean
2+
const validHasKey = <T extends object>(
3+
>validHasKey : <T extends object>(thing: T, key: string) => boolean
4+
><T extends object>( thing: T, key: string,): boolean => { return key in thing;} : <T extends object>(thing: T, key: string) => boolean
55

6-
thing: A,
7-
>thing : A
6+
thing: T,
7+
>thing : T
88

99
key: string,
1010
>key : string
@@ -13,16 +13,16 @@ const validHasKey = <A extends object>(
1313
return key in thing;
1414
>key in thing : boolean
1515
>key : string
16-
>thing : A
16+
>thing : T
1717

1818
};
1919

20-
const invalidHasKey = <A>(
21-
>invalidHasKey : <A>(thing: A, key: string) => boolean
22-
><A>( thing: A, key: string,): boolean => { return key in thing;} : <A>(thing: A, key: string) => boolean
20+
const alsoValidHasKey = <T>(
21+
>alsoValidHasKey : <T>(thing: T, key: string) => boolean
22+
><T>( thing: T, key: string,): boolean => { return key in thing;} : <T>(thing: T, key: string) => boolean
2323

24-
thing: A,
25-
>thing : A
24+
thing: T,
25+
>thing : T
2626

2727
key: string,
2828
>key : string
@@ -31,7 +31,23 @@ const invalidHasKey = <A>(
3131
return key in thing;
3232
>key in thing : boolean
3333
>key : string
34-
>thing : A
34+
>thing : T
3535

3636
};
3737

38+
function invalidHasKey<T extends string | number>(
39+
>invalidHasKey : <T extends string | number>(thing: T, key: string) => boolean
40+
41+
thing: T,
42+
>thing : T
43+
44+
key: string,
45+
>key : string
46+
47+
): boolean {
48+
return key in thing;
49+
>key in thing : boolean
50+
>key : string
51+
>thing : T
52+
}
53+

0 commit comments

Comments
 (0)