|
| 1 | +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. |
| 2 | +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. |
| 3 | +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. |
| 4 | +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. |
| 5 | +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. |
| 6 | +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. |
| 7 | +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. |
| 8 | +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. |
| 9 | +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. |
| 10 | +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. |
| 11 | +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. |
| 12 | + |
| 13 | + |
| 14 | +==== tests/cases/compiler/conditionalTypeDoesntSpinForever.ts (11 errors) ==== |
| 15 | + // A *self-contained* demonstration of the problem follows... |
| 16 | + // Test this by running `tsc --target es6` on the command-line, rather than through another build tool such as Gulp, Webpack, etc. |
| 17 | + |
| 18 | + export enum PubSubRecordIsStoredInRedisAsA { |
| 19 | + redisHash = "redisHash", |
| 20 | + jsonEncodedRedisString = "jsonEncodedRedisString" |
| 21 | + } |
| 22 | + |
| 23 | + export interface PubSubRecord<NAME extends string, RECORD, IDENTIFIER extends Partial<RECORD>> { |
| 24 | + name: NAME; |
| 25 | + record: RECORD; |
| 26 | + identifier: IDENTIFIER; |
| 27 | + storedAs: PubSubRecordIsStoredInRedisAsA; |
| 28 | + maxMsToWaitBeforePublishing: number; |
| 29 | + } |
| 30 | + |
| 31 | + type NameFieldConstructor<SO_FAR> = |
| 32 | + SO_FAR extends {name: any} ? {} : { |
| 33 | + name: <TYPE>(t?: TYPE) => BuildPubSubRecordType<SO_FAR & {name: TYPE}> |
| 34 | + } |
| 35 | + |
| 36 | + const buildNameFieldConstructor = <SO_FAR>(soFar: SO_FAR) => ( |
| 37 | + "name" in soFar ? {} : { |
| 38 | + ~~~~~ |
| 39 | +!!! error TS2361: The right-hand side of an 'in' expression must be of type 'any', an object type or a type parameter. |
| 40 | + name: <TYPE>(instance: TYPE = undefined) => |
| 41 | + buildPubSubRecordType(Object.assign({}, soFar, {name: instance as TYPE}) as SO_FAR & {name: TYPE}) as BuildPubSubRecordType<SO_FAR & {name: TYPE}> |
| 42 | + } |
| 43 | + ); |
| 44 | + |
| 45 | + type StoredAsConstructor<SO_FAR> = |
| 46 | + SO_FAR extends {storedAs: any} ? {} : { |
| 47 | + storedAsJsonEncodedRedisString: () => BuildPubSubRecordType<SO_FAR & {storedAs: PubSubRecordIsStoredInRedisAsA.jsonEncodedRedisString}>; |
| 48 | + storedRedisHash: () => BuildPubSubRecordType<SO_FAR & {storedAs: PubSubRecordIsStoredInRedisAsA.redisHash}>; |
| 49 | + } |
| 50 | + |
| 51 | + const buildStoredAsConstructor = <SO_FAR>(soFar: SO_FAR) => ( |
| 52 | + "storedAs" in soFar ? {} : { |
| 53 | + ~~~~~ |
| 54 | +!!! error TS2361: The right-hand side of an 'in' expression must be of type 'any', an object type or a type parameter. |
| 55 | + storedAsJsonEncodedRedisString: () => |
| 56 | + buildPubSubRecordType(Object.assign({}, soFar, {storedAs: PubSubRecordIsStoredInRedisAsA.jsonEncodedRedisString})) as |
| 57 | + BuildPubSubRecordType<SO_FAR & {storedAs: PubSubRecordIsStoredInRedisAsA.jsonEncodedRedisString}>, |
| 58 | + storedAsRedisHash: () => |
| 59 | + buildPubSubRecordType(Object.assign({}, soFar, {storedAs: PubSubRecordIsStoredInRedisAsA.redisHash})) as |
| 60 | + BuildPubSubRecordType<SO_FAR & {storedAs: PubSubRecordIsStoredInRedisAsA.redisHash}>, |
| 61 | + } |
| 62 | + ); |
| 63 | + |
| 64 | + type IdentifierFieldConstructor<SO_FAR> = |
| 65 | + SO_FAR extends {identifier: any} ? {} : |
| 66 | + SO_FAR extends {record: any} ? { |
| 67 | + identifier: <TYPE extends Partial<SO_FAR["record"]>>(t?: TYPE) => BuildPubSubRecordType<SO_FAR & {identifier: TYPE}> |
| 68 | + } : {} |
| 69 | + |
| 70 | + const buildIdentifierFieldConstructor = <SO_FAR>(soFar: SO_FAR) => ( |
| 71 | + "identifier" in soFar || (!("record" in soFar)) ? {} : { |
| 72 | + ~~~~~ |
| 73 | +!!! error TS2361: The right-hand side of an 'in' expression must be of type 'any', an object type or a type parameter. |
| 74 | + ~~~~~ |
| 75 | +!!! error TS2361: The right-hand side of an 'in' expression must be of type 'any', an object type or a type parameter. |
| 76 | + identifier: <TYPE>(instance: TYPE = undefined) => |
| 77 | + buildPubSubRecordType(Object.assign({}, soFar, {identifier: instance as TYPE}) as SO_FAR & {identifier: TYPE}) as BuildPubSubRecordType<SO_FAR & {identifier: TYPE}> |
| 78 | + } |
| 79 | + ); |
| 80 | + |
| 81 | + type RecordFieldConstructor<SO_FAR> = |
| 82 | + SO_FAR extends {record: any} ? {} : { |
| 83 | + record: <TYPE>(t?: TYPE) => BuildPubSubRecordType<SO_FAR & {record: TYPE}> |
| 84 | + } |
| 85 | + |
| 86 | + const buildRecordFieldConstructor = <SO_FAR>(soFar: SO_FAR) => ( |
| 87 | + "record" in soFar ? {} : { |
| 88 | + ~~~~~ |
| 89 | +!!! error TS2361: The right-hand side of an 'in' expression must be of type 'any', an object type or a type parameter. |
| 90 | + record: <TYPE>(instance: TYPE = undefined) => |
| 91 | + buildPubSubRecordType(Object.assign({}, soFar, {record: instance as TYPE}) as SO_FAR & {record: TYPE}) as BuildPubSubRecordType<SO_FAR & {record: TYPE}> |
| 92 | + } |
| 93 | + ); |
| 94 | + |
| 95 | + type MaxMsToWaitBeforePublishingFieldConstructor<SO_FAR> = |
| 96 | + SO_FAR extends {maxMsToWaitBeforePublishing: any} ? {} : { |
| 97 | + maxMsToWaitBeforePublishing: (t: number) => BuildPubSubRecordType<SO_FAR & {maxMsToWaitBeforePublishing: number}>, |
| 98 | + neverDelayPublishing: () => BuildPubSubRecordType<SO_FAR & {maxMsToWaitBeforePublishing: 0}>, |
| 99 | + } |
| 100 | + |
| 101 | + const buildMaxMsToWaitBeforePublishingFieldConstructor = <SO_FAR>(soFar: SO_FAR): MaxMsToWaitBeforePublishingFieldConstructor<SO_FAR> => ( |
| 102 | + "maxMsToWaitBeforePublishing" in soFar ? {} : { |
| 103 | + ~~~~~ |
| 104 | +!!! error TS2361: The right-hand side of an 'in' expression must be of type 'any', an object type or a type parameter. |
| 105 | + maxMsToWaitBeforePublishing: (instance: number = 0) => |
| 106 | + buildPubSubRecordType(Object.assign({}, soFar, {maxMsToWaitBeforePublishing: instance})) as BuildPubSubRecordType<SO_FAR & {maxMsToWaitBeforePublishing: number}>, |
| 107 | + neverDelayPublishing: () => |
| 108 | + buildPubSubRecordType(Object.assign({}, soFar, {maxMsToWaitBeforePublishing: 0})) as BuildPubSubRecordType<SO_FAR & {maxMsToWaitBeforePublishing: 0}>, |
| 109 | + } |
| 110 | + ) as MaxMsToWaitBeforePublishingFieldConstructor<SO_FAR>; |
| 111 | + |
| 112 | + type TypeConstructor<SO_FAR> = |
| 113 | + SO_FAR extends {identifier: any, record: any, maxMsToWaitBeforePublishing: number, storedAs: PubSubRecordIsStoredInRedisAsA} ? { |
| 114 | + type: SO_FAR, |
| 115 | + fields: Set<keyof SO_FAR>, |
| 116 | + hasField: (fieldName: string | number | symbol) => fieldName is keyof SO_FAR |
| 117 | + } : {} |
| 118 | + |
| 119 | + const buildType = <SO_FAR>(soFar: SO_FAR) => ( |
| 120 | + "identifier" in soFar && "object" in soFar && "maxMsToWaitBeforePublishing" in soFar && "PubSubRecordIsStoredInRedisAsA" in soFar ? {} : { |
| 121 | + ~~~~~ |
| 122 | +!!! error TS2361: The right-hand side of an 'in' expression must be of type 'any', an object type or a type parameter. |
| 123 | + ~~~~~ |
| 124 | +!!! error TS2361: The right-hand side of an 'in' expression must be of type 'any', an object type or a type parameter. |
| 125 | + ~~~~~ |
| 126 | +!!! error TS2361: The right-hand side of an 'in' expression must be of type 'any', an object type or a type parameter. |
| 127 | + ~~~~~ |
| 128 | +!!! error TS2361: The right-hand side of an 'in' expression must be of type 'any', an object type or a type parameter. |
| 129 | + type: soFar, |
| 130 | + fields: () => new Set(Object.keys(soFar) as (keyof SO_FAR)[]), |
| 131 | + hasField: (fieldName: string | number | symbol) => fieldName in soFar |
| 132 | + ~~~~~ |
| 133 | +!!! error TS2361: The right-hand side of an 'in' expression must be of type 'any', an object type or a type parameter. |
| 134 | + } |
| 135 | + ); |
| 136 | + |
| 137 | + type BuildPubSubRecordType<SO_FAR> = |
| 138 | + NameFieldConstructor<SO_FAR> & |
| 139 | + IdentifierFieldConstructor<SO_FAR> & |
| 140 | + RecordFieldConstructor<SO_FAR> & |
| 141 | + StoredAsConstructor<SO_FAR> & // infinite loop goes away when you comment out this line |
| 142 | + MaxMsToWaitBeforePublishingFieldConstructor<SO_FAR> & |
| 143 | + TypeConstructor<SO_FAR> |
| 144 | + |
| 145 | + const buildPubSubRecordType = <SO_FAR>(soFar: SO_FAR) => Object.assign( |
| 146 | + {}, |
| 147 | + buildNameFieldConstructor(soFar), |
| 148 | + buildIdentifierFieldConstructor(soFar), |
| 149 | + buildRecordFieldConstructor(soFar), |
| 150 | + buildStoredAsConstructor(soFar), |
| 151 | + buildMaxMsToWaitBeforePublishingFieldConstructor(soFar), |
| 152 | + buildType(soFar) |
| 153 | + ) as BuildPubSubRecordType<SO_FAR>; |
| 154 | + const PubSubRecordType = buildPubSubRecordType({}); |
0 commit comments