|
| 1 | +//// [tests/cases/compiler/enumBracketNotationInComputedProperties.ts] //// |
| 2 | + |
| 3 | +=== enumBracketNotationInComputedProperties.ts === |
| 4 | +// Test for issue #25083: Enum keys should be accepted as computed properties |
| 5 | +// when using bracket notation |
| 6 | + |
| 7 | +enum Type { |
| 8 | +>Type : Symbol(Type, Decl(enumBracketNotationInComputedProperties.ts, 0, 0)) |
| 9 | + |
| 10 | + Foo = 'foo', |
| 11 | +>Foo : Symbol(Type.Foo, Decl(enumBracketNotationInComputedProperties.ts, 3, 11)) |
| 12 | + |
| 13 | + Bar = 'bar', |
| 14 | +>Bar : Symbol(Type.Bar, Decl(enumBracketNotationInComputedProperties.ts, 4, 16)) |
| 15 | + |
| 16 | + '3x14' = '3x14', // Non-identifier name |
| 17 | +>'3x14' : Symbol(Type['3x14'], Decl(enumBracketNotationInComputedProperties.ts, 5, 16)) |
| 18 | + |
| 19 | + '0digit' = '0digit' // Starts with digit |
| 20 | +>'0digit' : Symbol(Type['0digit'], Decl(enumBracketNotationInComputedProperties.ts, 6, 20)) |
| 21 | +} |
| 22 | + |
| 23 | +// These should all work - dot notation (already works) |
| 24 | +type TypeMapDot = { |
| 25 | +>TypeMapDot : Symbol(TypeMapDot, Decl(enumBracketNotationInComputedProperties.ts, 8, 1)) |
| 26 | + |
| 27 | + [Type.Foo]: string; |
| 28 | +>[Type.Foo] : Symbol([Type.Foo], Decl(enumBracketNotationInComputedProperties.ts, 11, 19)) |
| 29 | +>Type.Foo : Symbol(Type.Foo, Decl(enumBracketNotationInComputedProperties.ts, 3, 11)) |
| 30 | +>Type : Symbol(Type, Decl(enumBracketNotationInComputedProperties.ts, 0, 0)) |
| 31 | +>Foo : Symbol(Type.Foo, Decl(enumBracketNotationInComputedProperties.ts, 3, 11)) |
| 32 | + |
| 33 | + [Type.Bar]: number; |
| 34 | +>[Type.Bar] : Symbol([Type.Bar], Decl(enumBracketNotationInComputedProperties.ts, 12, 23)) |
| 35 | +>Type.Bar : Symbol(Type.Bar, Decl(enumBracketNotationInComputedProperties.ts, 4, 16)) |
| 36 | +>Type : Symbol(Type, Decl(enumBracketNotationInComputedProperties.ts, 0, 0)) |
| 37 | +>Bar : Symbol(Type.Bar, Decl(enumBracketNotationInComputedProperties.ts, 4, 16)) |
| 38 | +} |
| 39 | + |
| 40 | +// These should also work - bracket notation with identifier names |
| 41 | +type TypeMapBracketIdentifier = { |
| 42 | +>TypeMapBracketIdentifier : Symbol(TypeMapBracketIdentifier, Decl(enumBracketNotationInComputedProperties.ts, 14, 1)) |
| 43 | + |
| 44 | + [Type['Foo']]: string; |
| 45 | +>[Type['Foo']] : Symbol([Type['Foo']], Decl(enumBracketNotationInComputedProperties.ts, 17, 33)) |
| 46 | +>Type : Symbol(Type, Decl(enumBracketNotationInComputedProperties.ts, 0, 0)) |
| 47 | +>'Foo' : Symbol(Type.Foo, Decl(enumBracketNotationInComputedProperties.ts, 3, 11)) |
| 48 | + |
| 49 | + [Type['Bar']]: number; |
| 50 | +>[Type['Bar']] : Symbol([Type['Bar']], Decl(enumBracketNotationInComputedProperties.ts, 18, 26)) |
| 51 | +>Type : Symbol(Type, Decl(enumBracketNotationInComputedProperties.ts, 0, 0)) |
| 52 | +>'Bar' : Symbol(Type.Bar, Decl(enumBracketNotationInComputedProperties.ts, 4, 16)) |
| 53 | +} |
| 54 | + |
| 55 | +// These should work - bracket notation with non-identifier names |
| 56 | +// This is the main fix for #25083 |
| 57 | +type TypeMapBracketNonIdentifier = { |
| 58 | +>TypeMapBracketNonIdentifier : Symbol(TypeMapBracketNonIdentifier, Decl(enumBracketNotationInComputedProperties.ts, 20, 1)) |
| 59 | + |
| 60 | + [Type['3x14']]: boolean; |
| 61 | +>[Type['3x14']] : Symbol([Type['3x14']], Decl(enumBracketNotationInComputedProperties.ts, 24, 36)) |
| 62 | +>Type : Symbol(Type, Decl(enumBracketNotationInComputedProperties.ts, 0, 0)) |
| 63 | +>'3x14' : Symbol(Type['3x14'], Decl(enumBracketNotationInComputedProperties.ts, 5, 16)) |
| 64 | + |
| 65 | + [Type['0digit']]: object; |
| 66 | +>[Type['0digit']] : Symbol([Type['0digit']], Decl(enumBracketNotationInComputedProperties.ts, 25, 28)) |
| 67 | +>Type : Symbol(Type, Decl(enumBracketNotationInComputedProperties.ts, 0, 0)) |
| 68 | +>'0digit' : Symbol(Type['0digit'], Decl(enumBracketNotationInComputedProperties.ts, 6, 20)) |
| 69 | +} |
| 70 | + |
| 71 | +// Mixed usage should also work |
| 72 | +type TypeMapMixed = { |
| 73 | +>TypeMapMixed : Symbol(TypeMapMixed, Decl(enumBracketNotationInComputedProperties.ts, 27, 1)) |
| 74 | + |
| 75 | + [Type.Foo]: string; |
| 76 | +>[Type.Foo] : Symbol([Type.Foo], Decl(enumBracketNotationInComputedProperties.ts, 30, 21)) |
| 77 | +>Type.Foo : Symbol(Type.Foo, Decl(enumBracketNotationInComputedProperties.ts, 3, 11)) |
| 78 | +>Type : Symbol(Type, Decl(enumBracketNotationInComputedProperties.ts, 0, 0)) |
| 79 | +>Foo : Symbol(Type.Foo, Decl(enumBracketNotationInComputedProperties.ts, 3, 11)) |
| 80 | + |
| 81 | + [Type['Bar']]: number; |
| 82 | +>[Type['Bar']] : Symbol([Type['Bar']], Decl(enumBracketNotationInComputedProperties.ts, 31, 23)) |
| 83 | +>Type : Symbol(Type, Decl(enumBracketNotationInComputedProperties.ts, 0, 0)) |
| 84 | +>'Bar' : Symbol(Type.Bar, Decl(enumBracketNotationInComputedProperties.ts, 4, 16)) |
| 85 | + |
| 86 | + [Type['3x14']]: boolean; |
| 87 | +>[Type['3x14']] : Symbol([Type['3x14']], Decl(enumBracketNotationInComputedProperties.ts, 32, 26)) |
| 88 | +>Type : Symbol(Type, Decl(enumBracketNotationInComputedProperties.ts, 0, 0)) |
| 89 | +>'3x14' : Symbol(Type['3x14'], Decl(enumBracketNotationInComputedProperties.ts, 5, 16)) |
| 90 | +} |
| 91 | + |
| 92 | +// Should work with const enums too |
| 93 | +const enum ConstType { |
| 94 | +>ConstType : Symbol(ConstType, Decl(enumBracketNotationInComputedProperties.ts, 34, 1)) |
| 95 | + |
| 96 | + Alpha = 'alpha', |
| 97 | +>Alpha : Symbol(ConstType.Alpha, Decl(enumBracketNotationInComputedProperties.ts, 37, 22)) |
| 98 | + |
| 99 | + '2beta' = '2beta' |
| 100 | +>'2beta' : Symbol(ConstType['2beta'], Decl(enumBracketNotationInComputedProperties.ts, 38, 20)) |
| 101 | +} |
| 102 | + |
| 103 | +type ConstTypeMap = { |
| 104 | +>ConstTypeMap : Symbol(ConstTypeMap, Decl(enumBracketNotationInComputedProperties.ts, 40, 1)) |
| 105 | + |
| 106 | + [ConstType.Alpha]: string; |
| 107 | +>[ConstType.Alpha] : Symbol([ConstType.Alpha], Decl(enumBracketNotationInComputedProperties.ts, 42, 21), Decl(enumBracketNotationInComputedProperties.ts, 43, 30)) |
| 108 | +>ConstType.Alpha : Symbol(ConstType.Alpha, Decl(enumBracketNotationInComputedProperties.ts, 37, 22)) |
| 109 | +>ConstType : Symbol(ConstType, Decl(enumBracketNotationInComputedProperties.ts, 34, 1)) |
| 110 | +>Alpha : Symbol(ConstType.Alpha, Decl(enumBracketNotationInComputedProperties.ts, 37, 22)) |
| 111 | + |
| 112 | + [ConstType['Alpha']]: string; |
| 113 | +>[ConstType['Alpha']] : Symbol([ConstType.Alpha], Decl(enumBracketNotationInComputedProperties.ts, 42, 21), Decl(enumBracketNotationInComputedProperties.ts, 43, 30)) |
| 114 | +>ConstType : Symbol(ConstType, Decl(enumBracketNotationInComputedProperties.ts, 34, 1)) |
| 115 | +>'Alpha' : Symbol(ConstType.Alpha, Decl(enumBracketNotationInComputedProperties.ts, 37, 22)) |
| 116 | + |
| 117 | + [ConstType['2beta']]: number; |
| 118 | +>[ConstType['2beta']] : Symbol([ConstType['2beta']], Decl(enumBracketNotationInComputedProperties.ts, 44, 33)) |
| 119 | +>ConstType : Symbol(ConstType, Decl(enumBracketNotationInComputedProperties.ts, 34, 1)) |
| 120 | +>'2beta' : Symbol(ConstType['2beta'], Decl(enumBracketNotationInComputedProperties.ts, 38, 20)) |
| 121 | +} |
| 122 | + |
| 123 | +// Verify that the types work correctly at runtime level |
| 124 | +const dotNotation: TypeMapDot = { |
| 125 | +>dotNotation : Symbol(dotNotation, Decl(enumBracketNotationInComputedProperties.ts, 49, 5)) |
| 126 | +>TypeMapDot : Symbol(TypeMapDot, Decl(enumBracketNotationInComputedProperties.ts, 8, 1)) |
| 127 | + |
| 128 | + [Type.Foo]: 'test', |
| 129 | +>[Type.Foo] : Symbol([Type.Foo], Decl(enumBracketNotationInComputedProperties.ts, 49, 33)) |
| 130 | +>Type.Foo : Symbol(Type.Foo, Decl(enumBracketNotationInComputedProperties.ts, 3, 11)) |
| 131 | +>Type : Symbol(Type, Decl(enumBracketNotationInComputedProperties.ts, 0, 0)) |
| 132 | +>Foo : Symbol(Type.Foo, Decl(enumBracketNotationInComputedProperties.ts, 3, 11)) |
| 133 | + |
| 134 | + [Type.Bar]: 123 |
| 135 | +>[Type.Bar] : Symbol([Type.Bar], Decl(enumBracketNotationInComputedProperties.ts, 50, 23)) |
| 136 | +>Type.Bar : Symbol(Type.Bar, Decl(enumBracketNotationInComputedProperties.ts, 4, 16)) |
| 137 | +>Type : Symbol(Type, Decl(enumBracketNotationInComputedProperties.ts, 0, 0)) |
| 138 | +>Bar : Symbol(Type.Bar, Decl(enumBracketNotationInComputedProperties.ts, 4, 16)) |
| 139 | + |
| 140 | +}; |
| 141 | + |
| 142 | +const bracketNotation: TypeMapBracketIdentifier = { |
| 143 | +>bracketNotation : Symbol(bracketNotation, Decl(enumBracketNotationInComputedProperties.ts, 54, 5)) |
| 144 | +>TypeMapBracketIdentifier : Symbol(TypeMapBracketIdentifier, Decl(enumBracketNotationInComputedProperties.ts, 14, 1)) |
| 145 | + |
| 146 | + [Type['Foo']]: 'test', |
| 147 | +>[Type['Foo']] : Symbol([Type['Foo']], Decl(enumBracketNotationInComputedProperties.ts, 54, 51)) |
| 148 | +>Type : Symbol(Type, Decl(enumBracketNotationInComputedProperties.ts, 0, 0)) |
| 149 | +>'Foo' : Symbol(Type.Foo, Decl(enumBracketNotationInComputedProperties.ts, 3, 11)) |
| 150 | + |
| 151 | + [Type['Bar']]: 456 |
| 152 | +>[Type['Bar']] : Symbol([Type['Bar']], Decl(enumBracketNotationInComputedProperties.ts, 55, 26)) |
| 153 | +>Type : Symbol(Type, Decl(enumBracketNotationInComputedProperties.ts, 0, 0)) |
| 154 | +>'Bar' : Symbol(Type.Bar, Decl(enumBracketNotationInComputedProperties.ts, 4, 16)) |
| 155 | + |
| 156 | +}; |
| 157 | + |
| 158 | +const nonIdentifierBracket: TypeMapBracketNonIdentifier = { |
| 159 | +>nonIdentifierBracket : Symbol(nonIdentifierBracket, Decl(enumBracketNotationInComputedProperties.ts, 59, 5)) |
| 160 | +>TypeMapBracketNonIdentifier : Symbol(TypeMapBracketNonIdentifier, Decl(enumBracketNotationInComputedProperties.ts, 20, 1)) |
| 161 | + |
| 162 | + [Type['3x14']]: true, |
| 163 | +>[Type['3x14']] : Symbol([Type['3x14']], Decl(enumBracketNotationInComputedProperties.ts, 59, 59)) |
| 164 | +>Type : Symbol(Type, Decl(enumBracketNotationInComputedProperties.ts, 0, 0)) |
| 165 | +>'3x14' : Symbol(Type['3x14'], Decl(enumBracketNotationInComputedProperties.ts, 5, 16)) |
| 166 | + |
| 167 | + [Type['0digit']]: {} |
| 168 | +>[Type['0digit']] : Symbol([Type['0digit']], Decl(enumBracketNotationInComputedProperties.ts, 60, 25)) |
| 169 | +>Type : Symbol(Type, Decl(enumBracketNotationInComputedProperties.ts, 0, 0)) |
| 170 | +>'0digit' : Symbol(Type['0digit'], Decl(enumBracketNotationInComputedProperties.ts, 6, 20)) |
| 171 | + |
| 172 | +}; |
| 173 | + |
| 174 | +const mixed: TypeMapMixed = { |
| 175 | +>mixed : Symbol(mixed, Decl(enumBracketNotationInComputedProperties.ts, 64, 5)) |
| 176 | +>TypeMapMixed : Symbol(TypeMapMixed, Decl(enumBracketNotationInComputedProperties.ts, 27, 1)) |
| 177 | + |
| 178 | + [Type.Foo]: 'test', |
| 179 | +>[Type.Foo] : Symbol([Type.Foo], Decl(enumBracketNotationInComputedProperties.ts, 64, 29)) |
| 180 | +>Type.Foo : Symbol(Type.Foo, Decl(enumBracketNotationInComputedProperties.ts, 3, 11)) |
| 181 | +>Type : Symbol(Type, Decl(enumBracketNotationInComputedProperties.ts, 0, 0)) |
| 182 | +>Foo : Symbol(Type.Foo, Decl(enumBracketNotationInComputedProperties.ts, 3, 11)) |
| 183 | + |
| 184 | + [Type['Bar']]: 789, |
| 185 | +>[Type['Bar']] : Symbol([Type['Bar']], Decl(enumBracketNotationInComputedProperties.ts, 65, 23)) |
| 186 | +>Type : Symbol(Type, Decl(enumBracketNotationInComputedProperties.ts, 0, 0)) |
| 187 | +>'Bar' : Symbol(Type.Bar, Decl(enumBracketNotationInComputedProperties.ts, 4, 16)) |
| 188 | + |
| 189 | + [Type['3x14']]: false |
| 190 | +>[Type['3x14']] : Symbol([Type['3x14']], Decl(enumBracketNotationInComputedProperties.ts, 66, 23)) |
| 191 | +>Type : Symbol(Type, Decl(enumBracketNotationInComputedProperties.ts, 0, 0)) |
| 192 | +>'3x14' : Symbol(Type['3x14'], Decl(enumBracketNotationInComputedProperties.ts, 5, 16)) |
| 193 | + |
| 194 | +}; |
| 195 | + |
0 commit comments