Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

'in' should not operate on primitive types #41928

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30002,7 +30002,7 @@ namespace ts {
isTypeAssignableToKind(leftType, TypeFlags.Index | TypeFlags.TemplateLiteral | TypeFlags.StringMapping | TypeFlags.TypeParameter))) {
error(left, Diagnostics.The_left_hand_side_of_an_in_expression_must_be_of_type_any_string_number_or_symbol);
}
if (!allTypesAssignableToKind(rightType, TypeFlags.NonPrimitive | TypeFlags.InstantiableNonPrimitive)) {
if (!isTypeAssignableToKind(rightType, TypeFlags.NonPrimitive)) {
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;
Expand Down
154 changes: 154 additions & 0 deletions tests/baselines/reference/conditionalTypeDoesntSpinForever.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
tests/cases/compiler/conditionalTypeDoesntSpinForever.ts(23,15): error TS2361: The right-hand side of an 'in' expression must be of type 'any', an object type or a type parameter.
tests/cases/compiler/conditionalTypeDoesntSpinForever.ts(36,19): error TS2361: The right-hand side of an 'in' expression must be of type 'any', an object type or a type parameter.
tests/cases/compiler/conditionalTypeDoesntSpinForever.ts(53,21): error TS2361: The right-hand side of an 'in' expression must be of type 'any', an object type or a type parameter.
tests/cases/compiler/conditionalTypeDoesntSpinForever.ts(53,45): error TS2361: The right-hand side of an 'in' expression must be of type 'any', an object type or a type parameter.
tests/cases/compiler/conditionalTypeDoesntSpinForever.ts(65,17): error TS2361: The right-hand side of an 'in' expression must be of type 'any', an object type or a type parameter.
tests/cases/compiler/conditionalTypeDoesntSpinForever.ts(78,38): error TS2361: The right-hand side of an 'in' expression must be of type 'any', an object type or a type parameter.
tests/cases/compiler/conditionalTypeDoesntSpinForever.ts(94,21): error TS2361: The right-hand side of an 'in' expression must be of type 'any', an object type or a type parameter.
tests/cases/compiler/conditionalTypeDoesntSpinForever.ts(94,42): error TS2361: The right-hand side of an 'in' expression must be of type 'any', an object type or a type parameter.
tests/cases/compiler/conditionalTypeDoesntSpinForever.ts(94,84): error TS2361: The right-hand side of an 'in' expression must be of type 'any', an object type or a type parameter.
tests/cases/compiler/conditionalTypeDoesntSpinForever.ts(94,129): error TS2361: The right-hand side of an 'in' expression must be of type 'any', an object type or a type parameter.
tests/cases/compiler/conditionalTypeDoesntSpinForever.ts(97,71): error TS2361: The right-hand side of an 'in' expression must be of type 'any', an object type or a type parameter.


==== tests/cases/compiler/conditionalTypeDoesntSpinForever.ts (11 errors) ====
// A *self-contained* demonstration of the problem follows...
// Test this by running `tsc --target es6` on the command-line, rather than through another build tool such as Gulp, Webpack, etc.

export enum PubSubRecordIsStoredInRedisAsA {
redisHash = "redisHash",
jsonEncodedRedisString = "jsonEncodedRedisString"
}

export interface PubSubRecord<NAME extends string, RECORD, IDENTIFIER extends Partial<RECORD>> {
name: NAME;
record: RECORD;
identifier: IDENTIFIER;
storedAs: PubSubRecordIsStoredInRedisAsA;
maxMsToWaitBeforePublishing: number;
}

type NameFieldConstructor<SO_FAR> =
SO_FAR extends {name: any} ? {} : {
name: <TYPE>(t?: TYPE) => BuildPubSubRecordType<SO_FAR & {name: TYPE}>
}

const buildNameFieldConstructor = <SO_FAR>(soFar: SO_FAR) => (
"name" in soFar ? {} : {
~~~~~
!!! error TS2361: The right-hand side of an 'in' expression must be of type 'any', an object type or a type parameter.
name: <TYPE>(instance: TYPE = undefined) =>
buildPubSubRecordType(Object.assign({}, soFar, {name: instance as TYPE}) as SO_FAR & {name: TYPE}) as BuildPubSubRecordType<SO_FAR & {name: TYPE}>
}
);

type StoredAsConstructor<SO_FAR> =
SO_FAR extends {storedAs: any} ? {} : {
storedAsJsonEncodedRedisString: () => BuildPubSubRecordType<SO_FAR & {storedAs: PubSubRecordIsStoredInRedisAsA.jsonEncodedRedisString}>;
storedRedisHash: () => BuildPubSubRecordType<SO_FAR & {storedAs: PubSubRecordIsStoredInRedisAsA.redisHash}>;
}

const buildStoredAsConstructor = <SO_FAR>(soFar: SO_FAR) => (
"storedAs" in soFar ? {} : {
~~~~~
!!! error TS2361: The right-hand side of an 'in' expression must be of type 'any', an object type or a type parameter.
storedAsJsonEncodedRedisString: () =>
buildPubSubRecordType(Object.assign({}, soFar, {storedAs: PubSubRecordIsStoredInRedisAsA.jsonEncodedRedisString})) as
BuildPubSubRecordType<SO_FAR & {storedAs: PubSubRecordIsStoredInRedisAsA.jsonEncodedRedisString}>,
storedAsRedisHash: () =>
buildPubSubRecordType(Object.assign({}, soFar, {storedAs: PubSubRecordIsStoredInRedisAsA.redisHash})) as
BuildPubSubRecordType<SO_FAR & {storedAs: PubSubRecordIsStoredInRedisAsA.redisHash}>,
}
);

type IdentifierFieldConstructor<SO_FAR> =
SO_FAR extends {identifier: any} ? {} :
SO_FAR extends {record: any} ? {
identifier: <TYPE extends Partial<SO_FAR["record"]>>(t?: TYPE) => BuildPubSubRecordType<SO_FAR & {identifier: TYPE}>
} : {}

const buildIdentifierFieldConstructor = <SO_FAR>(soFar: SO_FAR) => (
"identifier" in soFar || (!("record" in soFar)) ? {} : {
~~~~~
!!! error TS2361: The right-hand side of an 'in' expression must be of type 'any', an object type or a type parameter.
~~~~~
!!! error TS2361: The right-hand side of an 'in' expression must be of type 'any', an object type or a type parameter.
identifier: <TYPE>(instance: TYPE = undefined) =>
buildPubSubRecordType(Object.assign({}, soFar, {identifier: instance as TYPE}) as SO_FAR & {identifier: TYPE}) as BuildPubSubRecordType<SO_FAR & {identifier: TYPE}>
}
);

type RecordFieldConstructor<SO_FAR> =
SO_FAR extends {record: any} ? {} : {
record: <TYPE>(t?: TYPE) => BuildPubSubRecordType<SO_FAR & {record: TYPE}>
}

const buildRecordFieldConstructor = <SO_FAR>(soFar: SO_FAR) => (
"record" in soFar ? {} : {
~~~~~
!!! error TS2361: The right-hand side of an 'in' expression must be of type 'any', an object type or a type parameter.
record: <TYPE>(instance: TYPE = undefined) =>
buildPubSubRecordType(Object.assign({}, soFar, {record: instance as TYPE}) as SO_FAR & {record: TYPE}) as BuildPubSubRecordType<SO_FAR & {record: TYPE}>
}
);

type MaxMsToWaitBeforePublishingFieldConstructor<SO_FAR> =
SO_FAR extends {maxMsToWaitBeforePublishing: any} ? {} : {
maxMsToWaitBeforePublishing: (t: number) => BuildPubSubRecordType<SO_FAR & {maxMsToWaitBeforePublishing: number}>,
neverDelayPublishing: () => BuildPubSubRecordType<SO_FAR & {maxMsToWaitBeforePublishing: 0}>,
}

const buildMaxMsToWaitBeforePublishingFieldConstructor = <SO_FAR>(soFar: SO_FAR): MaxMsToWaitBeforePublishingFieldConstructor<SO_FAR> => (
"maxMsToWaitBeforePublishing" in soFar ? {} : {
~~~~~
!!! error TS2361: The right-hand side of an 'in' expression must be of type 'any', an object type or a type parameter.
maxMsToWaitBeforePublishing: (instance: number = 0) =>
buildPubSubRecordType(Object.assign({}, soFar, {maxMsToWaitBeforePublishing: instance})) as BuildPubSubRecordType<SO_FAR & {maxMsToWaitBeforePublishing: number}>,
neverDelayPublishing: () =>
buildPubSubRecordType(Object.assign({}, soFar, {maxMsToWaitBeforePublishing: 0})) as BuildPubSubRecordType<SO_FAR & {maxMsToWaitBeforePublishing: 0}>,
}
) as MaxMsToWaitBeforePublishingFieldConstructor<SO_FAR>;

type TypeConstructor<SO_FAR> =
SO_FAR extends {identifier: any, record: any, maxMsToWaitBeforePublishing: number, storedAs: PubSubRecordIsStoredInRedisAsA} ? {
type: SO_FAR,
fields: Set<keyof SO_FAR>,
hasField: (fieldName: string | number | symbol) => fieldName is keyof SO_FAR
} : {}

const buildType = <SO_FAR>(soFar: SO_FAR) => (
"identifier" in soFar && "object" in soFar && "maxMsToWaitBeforePublishing" in soFar && "PubSubRecordIsStoredInRedisAsA" in soFar ? {} : {
~~~~~
!!! error TS2361: The right-hand side of an 'in' expression must be of type 'any', an object type or a type parameter.
~~~~~
!!! error TS2361: The right-hand side of an 'in' expression must be of type 'any', an object type or a type parameter.
~~~~~
!!! error TS2361: The right-hand side of an 'in' expression must be of type 'any', an object type or a type parameter.
~~~~~
!!! error TS2361: The right-hand side of an 'in' expression must be of type 'any', an object type or a type parameter.
type: soFar,
fields: () => new Set(Object.keys(soFar) as (keyof SO_FAR)[]),
hasField: (fieldName: string | number | symbol) => fieldName in soFar
~~~~~
!!! error TS2361: The right-hand side of an 'in' expression must be of type 'any', an object type or a type parameter.
}
);

type BuildPubSubRecordType<SO_FAR> =
NameFieldConstructor<SO_FAR> &
IdentifierFieldConstructor<SO_FAR> &
RecordFieldConstructor<SO_FAR> &
StoredAsConstructor<SO_FAR> & // infinite loop goes away when you comment out this line
MaxMsToWaitBeforePublishingFieldConstructor<SO_FAR> &
TypeConstructor<SO_FAR>

const buildPubSubRecordType = <SO_FAR>(soFar: SO_FAR) => Object.assign(
{},
buildNameFieldConstructor(soFar),
buildIdentifierFieldConstructor(soFar),
buildRecordFieldConstructor(soFar),
buildStoredAsConstructor(soFar),
buildMaxMsToWaitBeforePublishingFieldConstructor(soFar),
buildType(soFar)
) as BuildPubSubRecordType<SO_FAR>;
const PubSubRecordType = buildPubSubRecordType({});
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
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.


==== tests/cases/compiler/inDoesNotOperateOnPrimitiveTypes.ts (1 errors) ====
const validHasKey = <A extends object>(
thing: A,
key: string,
): boolean => {
return key in thing;
};

const invalidHasKey = <A>(
thing: A,
key: string,
): boolean => {
return key in thing;
~~~~~
!!! error TS2361: The right-hand side of an 'in' expression must be of type 'any', an object type or a type parameter.
};

23 changes: 23 additions & 0 deletions tests/baselines/reference/inDoesNotOperateOnPrimitiveTypes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//// [inDoesNotOperateOnPrimitiveTypes.ts]
const validHasKey = <A extends object>(
thing: A,
key: string,
): boolean => {
return key in thing;
};

const invalidHasKey = <A>(
thing: A,
key: string,
): boolean => {
return key in thing;
};


//// [inDoesNotOperateOnPrimitiveTypes.js]
var validHasKey = function (thing, key) {
return key in thing;
};
var invalidHasKey = function (thing, key) {
return key in thing;
};
37 changes: 37 additions & 0 deletions tests/baselines/reference/inDoesNotOperateOnPrimitiveTypes.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
=== tests/cases/compiler/inDoesNotOperateOnPrimitiveTypes.ts ===
const validHasKey = <A extends object>(
>validHasKey : Symbol(validHasKey, Decl(inDoesNotOperateOnPrimitiveTypes.ts, 0, 5))
>A : Symbol(A, Decl(inDoesNotOperateOnPrimitiveTypes.ts, 0, 21))

thing: A,
>thing : Symbol(thing, Decl(inDoesNotOperateOnPrimitiveTypes.ts, 0, 39))
>A : Symbol(A, Decl(inDoesNotOperateOnPrimitiveTypes.ts, 0, 21))

key: string,
>key : Symbol(key, Decl(inDoesNotOperateOnPrimitiveTypes.ts, 1, 11))

): boolean => {
return key in thing;
>key : Symbol(key, Decl(inDoesNotOperateOnPrimitiveTypes.ts, 1, 11))
>thing : Symbol(thing, Decl(inDoesNotOperateOnPrimitiveTypes.ts, 0, 39))

};

const invalidHasKey = <A>(
>invalidHasKey : Symbol(invalidHasKey, Decl(inDoesNotOperateOnPrimitiveTypes.ts, 7, 5))
>A : Symbol(A, Decl(inDoesNotOperateOnPrimitiveTypes.ts, 7, 23))

thing: A,
>thing : Symbol(thing, Decl(inDoesNotOperateOnPrimitiveTypes.ts, 7, 26))
>A : Symbol(A, Decl(inDoesNotOperateOnPrimitiveTypes.ts, 7, 23))

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

): boolean => {
return key in thing;
>key : Symbol(key, Decl(inDoesNotOperateOnPrimitiveTypes.ts, 8, 11))
>thing : Symbol(thing, Decl(inDoesNotOperateOnPrimitiveTypes.ts, 7, 26))

};

37 changes: 37 additions & 0 deletions tests/baselines/reference/inDoesNotOperateOnPrimitiveTypes.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
=== tests/cases/compiler/inDoesNotOperateOnPrimitiveTypes.ts ===
const validHasKey = <A extends object>(
>validHasKey : <A extends object>(thing: A, key: string) => boolean
><A extends object>( thing: A, key: string,): boolean => { return key in thing;} : <A extends object>(thing: A, key: string) => boolean

thing: A,
>thing : A

key: string,
>key : string

): boolean => {
return key in thing;
>key in thing : boolean
>key : string
>thing : A

};

const invalidHasKey = <A>(
>invalidHasKey : <A>(thing: A, key: string) => boolean
><A>( thing: A, key: string,): boolean => { return key in thing;} : <A>(thing: A, key: string) => boolean

thing: A,
>thing : A

key: string,
>key : string

): boolean => {
return key in thing;
>key in thing : boolean
>key : string
>thing : A

};

58 changes: 58 additions & 0 deletions tests/baselines/reference/inOperatorWithValidOperands.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
tests/cases/conformance/expressions/binaryOperators/inOperator/inOperatorWithValidOperands.ts(26,20): error TS2361: The right-hand side of an 'in' expression must be of type 'any', an object type or a type parameter.
tests/cases/conformance/expressions/binaryOperators/inOperator/inOperatorWithValidOperands.ts(30,20): error TS2361: The right-hand side of an 'in' expression must be of type 'any', an object type or a type parameter.
tests/cases/conformance/expressions/binaryOperators/inOperator/inOperatorWithValidOperands.ts(34,20): error TS2361: The right-hand side of an 'in' expression must be of type 'any', an object type or a type parameter.


==== tests/cases/conformance/expressions/binaryOperators/inOperator/inOperatorWithValidOperands.ts (3 errors) ====
var x: any;

// valid left operands
// the left operand is required to be of type Any, the String primitive type, or the Number primitive type
var a1: string;
var a2: number;
var a3: string | number | symbol;
var a4: any;

var ra1 = x in x;
var ra2 = a1 in x;
var ra3 = a2 in x;
var ra4 = '' in x;
var ra5 = 0 in x;
var ra6 = a3 in x;
var ra7 = a4 in x;

// valid right operands
// the right operand is required to be of type Any, an object type, or a type parameter type
var b1: {};

var rb1 = x in b1;
var rb2 = x in {};

function foo<T>(t: T) {
var rb3 = x in t;
~
!!! error TS2361: The right-hand side of an 'in' expression must be of type 'any', an object type or a type parameter.
}

function unionCase<T, U>(t: T | U) {
var rb4 = x in t;
~
!!! error TS2361: The right-hand side of an 'in' expression must be of type 'any', an object type or a type parameter.
}

function unionCase2<T>(t: T | object) {
var rb5 = x in t;
~
!!! error TS2361: The right-hand side of an 'in' expression must be of type 'any', an object type or a type parameter.
}

interface X { x: number }
interface Y { y: number }

var c1: X | Y;
var c2: X;
var c3: Y;

var rc1 = x in c1;
var rc2 = x in (c2 || c3);

Loading