|
| 1 | +=== tests/cases/conformance/types/union/contextualTypeWithUnionTypeIndexSignatures.ts === |
| 2 | +//When used as a contextual type, a union type U has those members that are present in any of |
| 3 | +// its constituent types, with types that are unions of the respective members in the constituent types. |
| 4 | +interface SomeType { |
| 5 | +>SomeType : SomeType |
| 6 | + |
| 7 | + (a: number): number; |
| 8 | +>a : number |
| 9 | +} |
| 10 | +interface SomeType2 { |
| 11 | +>SomeType2 : SomeType2 |
| 12 | + |
| 13 | + (a: number): string; |
| 14 | +>a : number |
| 15 | +} |
| 16 | + |
| 17 | +interface IWithNoStringIndexSignature { |
| 18 | +>IWithNoStringIndexSignature : IWithNoStringIndexSignature |
| 19 | + |
| 20 | + foo: string; |
| 21 | +>foo : string |
| 22 | +} |
| 23 | +interface IWithNoNumberIndexSignature { |
| 24 | +>IWithNoNumberIndexSignature : IWithNoNumberIndexSignature |
| 25 | + |
| 26 | + 0: string; |
| 27 | +} |
| 28 | +interface IWithStringIndexSignature1 { |
| 29 | +>IWithStringIndexSignature1 : IWithStringIndexSignature1 |
| 30 | + |
| 31 | + [a: string]: SomeType; |
| 32 | +>a : string |
| 33 | +>SomeType : SomeType |
| 34 | +} |
| 35 | +interface IWithStringIndexSignature2 { |
| 36 | +>IWithStringIndexSignature2 : IWithStringIndexSignature2 |
| 37 | + |
| 38 | + [a: string]: SomeType2; |
| 39 | +>a : string |
| 40 | +>SomeType2 : SomeType2 |
| 41 | +} |
| 42 | +interface IWithNumberIndexSignature1 { |
| 43 | +>IWithNumberIndexSignature1 : IWithNumberIndexSignature1 |
| 44 | + |
| 45 | + [a: number]: SomeType; |
| 46 | +>a : number |
| 47 | +>SomeType : SomeType |
| 48 | +} |
| 49 | +interface IWithNumberIndexSignature2 { |
| 50 | +>IWithNumberIndexSignature2 : IWithNumberIndexSignature2 |
| 51 | + |
| 52 | + [a: number]: SomeType2; |
| 53 | +>a : number |
| 54 | +>SomeType2 : SomeType2 |
| 55 | +} |
| 56 | + |
| 57 | +// When an object literal is contextually typed by a type that includes a string index signature, |
| 58 | +// the resulting type of the object literal includes a string index signature with the union type of |
| 59 | +// the types of the properties declared in the object literal, or the Undefined type if the object literal |
| 60 | +// is empty.Likewise, when an object literal is contextually typed by a type that includes a numeric index |
| 61 | +// signature, the resulting type of the object literal includes a numeric index signature with the union type |
| 62 | +// of the types of the numerically named properties(section 3.7.4) declared in the object literal, |
| 63 | +// or the Undefined type if the object literal declares no numerically named properties. |
| 64 | + |
| 65 | +// Let S be the set of types in U that has a string index signature. |
| 66 | +// If S is not empty, U has a string index signature of a union type of |
| 67 | +// the types of the string index signatures from each type in S. |
| 68 | +var x: IWithNoStringIndexSignature | IWithStringIndexSignature1 = { z: a => a }; // a should be number |
| 69 | +>x : IWithNoStringIndexSignature | IWithStringIndexSignature1 |
| 70 | +>IWithNoStringIndexSignature : IWithNoStringIndexSignature |
| 71 | +>IWithStringIndexSignature1 : IWithStringIndexSignature1 |
| 72 | +>{ z: a => a } : { [x: string]: (a: number) => number; z: (a: number) => number; } |
| 73 | +>z : (a: number) => number |
| 74 | +>a => a : (a: number) => number |
| 75 | +>a : number |
| 76 | +>a : number |
| 77 | + |
| 78 | +var x: IWithNoStringIndexSignature | IWithStringIndexSignature1 = { foo: a => a }; // a should be any |
| 79 | +>x : IWithNoStringIndexSignature | IWithStringIndexSignature1 |
| 80 | +>IWithNoStringIndexSignature : IWithNoStringIndexSignature |
| 81 | +>IWithStringIndexSignature1 : IWithStringIndexSignature1 |
| 82 | +>{ foo: a => a } : { [x: string]: (a: any) => any; foo: (a: any) => any; } |
| 83 | +>foo : (a: any) => any |
| 84 | +>a => a : (a: any) => any |
| 85 | +>a : any |
| 86 | +>a : any |
| 87 | + |
| 88 | +var x: IWithNoStringIndexSignature | IWithStringIndexSignature1 = { foo: "hello" }; |
| 89 | +>x : IWithNoStringIndexSignature | IWithStringIndexSignature1 |
| 90 | +>IWithNoStringIndexSignature : IWithNoStringIndexSignature |
| 91 | +>IWithStringIndexSignature1 : IWithStringIndexSignature1 |
| 92 | +>{ foo: "hello" } : { [x: string]: string; foo: string; } |
| 93 | +>foo : string |
| 94 | + |
| 95 | +var x2: IWithStringIndexSignature1 | IWithStringIndexSignature2 = { z: a => a.toString() }; // a should be number |
| 96 | +>x2 : IWithStringIndexSignature1 | IWithStringIndexSignature2 |
| 97 | +>IWithStringIndexSignature1 : IWithStringIndexSignature1 |
| 98 | +>IWithStringIndexSignature2 : IWithStringIndexSignature2 |
| 99 | +>{ z: a => a.toString() } : { [x: string]: (a: number) => string; z: (a: number) => string; } |
| 100 | +>z : (a: number) => string |
| 101 | +>a => a.toString() : (a: number) => string |
| 102 | +>a : number |
| 103 | +>a.toString() : string |
| 104 | +>a.toString : (radix?: number) => string |
| 105 | +>a : number |
| 106 | +>toString : (radix?: number) => string |
| 107 | + |
| 108 | +var x2: IWithStringIndexSignature1 | IWithStringIndexSignature2 = { z: a => a }; // a should be number |
| 109 | +>x2 : IWithStringIndexSignature1 | IWithStringIndexSignature2 |
| 110 | +>IWithStringIndexSignature1 : IWithStringIndexSignature1 |
| 111 | +>IWithStringIndexSignature2 : IWithStringIndexSignature2 |
| 112 | +>{ z: a => a } : { [x: string]: (a: number) => number; z: (a: number) => number; } |
| 113 | +>z : (a: number) => number |
| 114 | +>a => a : (a: number) => number |
| 115 | +>a : number |
| 116 | +>a : number |
| 117 | + |
| 118 | + |
| 119 | +// Let S be the set of types in U that has a numeric index signature. |
| 120 | +// If S is not empty, U has a numeric index signature of a union type of |
| 121 | +// the types of the numeric index signatures from each type in S. |
| 122 | +var x3: IWithNoNumberIndexSignature | IWithNumberIndexSignature1 = { 1: a => a }; // a should be number |
| 123 | +>x3 : IWithNoNumberIndexSignature | IWithNumberIndexSignature1 |
| 124 | +>IWithNoNumberIndexSignature : IWithNoNumberIndexSignature |
| 125 | +>IWithNumberIndexSignature1 : IWithNumberIndexSignature1 |
| 126 | +>{ 1: a => a } : { [x: number]: (a: number) => number; 1: (a: number) => number; } |
| 127 | +>a => a : (a: number) => number |
| 128 | +>a : number |
| 129 | +>a : number |
| 130 | + |
| 131 | +var x3: IWithNoNumberIndexSignature | IWithNumberIndexSignature1 = { 0: a => a }; // a should be any |
| 132 | +>x3 : IWithNoNumberIndexSignature | IWithNumberIndexSignature1 |
| 133 | +>IWithNoNumberIndexSignature : IWithNoNumberIndexSignature |
| 134 | +>IWithNumberIndexSignature1 : IWithNumberIndexSignature1 |
| 135 | +>{ 0: a => a } : { [x: number]: (a: any) => any; 0: (a: any) => any; } |
| 136 | +>a => a : (a: any) => any |
| 137 | +>a : any |
| 138 | +>a : any |
| 139 | + |
| 140 | +var x3: IWithNoNumberIndexSignature | IWithNumberIndexSignature1 = { 0: "hello" }; |
| 141 | +>x3 : IWithNoNumberIndexSignature | IWithNumberIndexSignature1 |
| 142 | +>IWithNoNumberIndexSignature : IWithNoNumberIndexSignature |
| 143 | +>IWithNumberIndexSignature1 : IWithNumberIndexSignature1 |
| 144 | +>{ 0: "hello" } : { [x: number]: string; 0: string; } |
| 145 | + |
| 146 | +var x4: IWithNumberIndexSignature1 | IWithNumberIndexSignature2 = { 1: a => a.toString() }; // a should be number |
| 147 | +>x4 : IWithNumberIndexSignature1 | IWithNumberIndexSignature2 |
| 148 | +>IWithNumberIndexSignature1 : IWithNumberIndexSignature1 |
| 149 | +>IWithNumberIndexSignature2 : IWithNumberIndexSignature2 |
| 150 | +>{ 1: a => a.toString() } : { [x: number]: (a: number) => string; 1: (a: number) => string; } |
| 151 | +>a => a.toString() : (a: number) => string |
| 152 | +>a : number |
| 153 | +>a.toString() : string |
| 154 | +>a.toString : (radix?: number) => string |
| 155 | +>a : number |
| 156 | +>toString : (radix?: number) => string |
| 157 | + |
| 158 | +var x4: IWithNumberIndexSignature1 | IWithNumberIndexSignature2 = { 1: a => a }; // a should be number |
| 159 | +>x4 : IWithNumberIndexSignature1 | IWithNumberIndexSignature2 |
| 160 | +>IWithNumberIndexSignature1 : IWithNumberIndexSignature1 |
| 161 | +>IWithNumberIndexSignature2 : IWithNumberIndexSignature2 |
| 162 | +>{ 1: a => a } : { [x: number]: (a: number) => number; 1: (a: number) => number; } |
| 163 | +>a => a : (a: number) => number |
| 164 | +>a : number |
| 165 | +>a : number |
| 166 | + |
0 commit comments