tests/cases/compiler/inDoesNotOperateOnPrimitiveTypes.ts(12,17): error TS2322: Type 'T' is not assignable to type 'object'. tests/cases/compiler/inDoesNotOperateOnPrimitiveTypes.ts(19,17): error TS2322: Type 'T' is not assignable to type 'object'. Type 'string | number' is not assignable to type 'object'. Type 'string' is not assignable to type 'object'. tests/cases/compiler/inDoesNotOperateOnPrimitiveTypes.ts(23,12): error TS2322: Type 'T | U' is not assignable to type 'object'. Type 'T' is not assignable to type 'object'. Type 'string | number' is not assignable to type 'object'. Type 'string' is not assignable to type 'object'. tests/cases/compiler/inDoesNotOperateOnPrimitiveTypes.ts(27,12): error TS2322: Type 'T | U' is not assignable to type 'object'. Type 'U' is not assignable to type 'object'. Type 'string | number' is not assignable to type 'object'. Type 'string' is not assignable to type 'object'. tests/cases/compiler/inDoesNotOperateOnPrimitiveTypes.ts(34,12): error TS2322: Type 'string | number | T' is not assignable to type 'object'. Type 'string' is not assignable to type 'object'. tests/cases/compiler/inDoesNotOperateOnPrimitiveTypes.ts(36,14): error TS2322: Type 'T' is not assignable to type 'object'. tests/cases/compiler/inDoesNotOperateOnPrimitiveTypes.ts(41,12): error TS2322: Type 'T' is not assignable to type 'object'. Type 'object | "hello"' is not assignable to type 'object'. Type 'string' is not assignable to type 'object'. tests/cases/compiler/inDoesNotOperateOnPrimitiveTypes.ts(53,14): error TS2322: Type 'T | U' is not assignable to type 'object'. Type 'T' is not assignable to type 'object'. Type 'string | object' is not assignable to type 'object'. Type 'string' is not assignable to type 'object'. tests/cases/compiler/inDoesNotOperateOnPrimitiveTypes.ts(60,12): error TS2322: Type 'T & U' is not assignable to type 'object'. tests/cases/compiler/inDoesNotOperateOnPrimitiveTypes.ts(64,12): error TS2322: Type 'T & (0 | 1 | 2)' is not assignable to type 'object'. Type 'T & 0' is not assignable to type 'object'. ==== tests/cases/compiler/inDoesNotOperateOnPrimitiveTypes.ts (10 errors) ==== const validHasKey = ( thing: T, key: string, ): boolean => { return key in thing; // Ok }; const alsoValidHasKey = ( thing: T, key: string, ): boolean => { return key in thing; // Ok (as T may be instantiated with a valid type) ~~~~~ !!! error TS2322: Type 'T' is not assignable to type 'object'. !!! related TS2208 tests/cases/compiler/inDoesNotOperateOnPrimitiveTypes.ts:8:26: This type parameter might need an `extends object` constraint. }; function invalidHasKey( thing: T, key: string, ): boolean { return key in thing; // Error (because all possible instantiations are errors) ~~~~~ !!! error TS2322: Type 'T' is not assignable to type 'object'. !!! error TS2322: Type 'string | number' is not assignable to type 'object'. !!! error TS2322: Type 'string' is not assignable to type 'object'. } function union1(thing: T | U) { "key" in thing; // Error (because all possible instantiations are errors) ~~~~~ !!! error TS2322: Type 'T | U' is not assignable to type 'object'. !!! error TS2322: Type 'T' is not assignable to type 'object'. !!! error TS2322: Type 'string | number' is not assignable to type 'object'. !!! error TS2322: Type 'string' is not assignable to type 'object'. } function union2(thing: T | U) { "key" in thing; // Error (because narrowing is possible) ~~~~~ !!! error TS2322: Type 'T | U' is not assignable to type 'object'. !!! error TS2322: Type 'U' is not assignable to type 'object'. !!! error TS2322: Type 'string | number' is not assignable to type 'object'. !!! error TS2322: Type 'string' is not assignable to type 'object'. if (typeof thing === "object") { "key" in thing; // Ok } } function union3(thing: T | string | number) { "key" in thing; // Error (because narrowing is possible) ~~~~~ !!! error TS2322: Type 'string | number | T' is not assignable to type 'object'. !!! error TS2322: Type 'string' is not assignable to type 'object'. if (typeof thing !== "string" && typeof thing !== "number") { "key" in thing; // Ok (because further narrowing is impossible) ~~~~~ !!! error TS2322: Type 'T' is not assignable to type 'object'. !!! related TS2208 tests/cases/compiler/inDoesNotOperateOnPrimitiveTypes.ts:33:17: This type parameter might need an `extends object` constraint. } } function union4(thing: T) { "key" in thing; // Ok (because narrowing is impossible) ~~~~~ !!! error TS2322: Type 'T' is not assignable to type 'object'. !!! error TS2322: Type 'object | "hello"' is not assignable to type 'object'. !!! error TS2322: Type 'string' is not assignable to type 'object'. } function union5(p: T | U) { // For consistency, this should probably not be an error, because useful // narrowing is impossible. However, this is exceptionally strange input, // and it adds a lot of complexity to distinguish between a `T | U` where // one constraint is non-primitive and the other is primitive and a `T | U` // like this where both constraints have primitive and non-primitive // constitutents. Also, the strictly sound behavior would be to error // here, which is what's happening, so "fixing" this by suppressing the // error seems very low-value. "key" in p; ~ !!! error TS2322: Type 'T | U' is not assignable to type 'object'. !!! error TS2322: Type 'T' is not assignable to type 'object'. !!! error TS2322: Type 'string | object' is not assignable to type 'object'. !!! error TS2322: Type 'string' is not assignable to type 'object'. if (typeof p === "object") { "key" in p; } } function intersection1(thing: T & U) { "key" in thing; // Error (because all possible instantiations are errors) ~~~~~ !!! error TS2322: Type 'T & U' is not assignable to type 'object'. } function intersection2(thing: T & (0 | 1 | 2)) { "key" in thing; // Error (because all possible instantations are errors) ~~~~~ !!! error TS2322: Type 'T & (0 | 1 | 2)' is not assignable to type 'object'. !!! error TS2322: Type 'T & 0' is not assignable to type 'object'. }