|
56 | 56 | - [Things That Don't Work](#things-that-dont-work)
|
57 | 57 | - [You should emit classes like this so they have real private members](#you-should-emit-classes-like-this-so-they-have-real-private-members)
|
58 | 58 | - [You should emit classes like this so they don't lose `this` in callbacks](#you-should-emit-classes-like-this-so-they-dont-lose-this-in-callbacks)
|
59 |
| - - [Why can't I access arbitrary properties on a type with a string indexer?](#why-cant-i-access-arbitrary-properties-on-a-type-with-a-string-indexer) |
60 | 59 | - [You should have some class initialization which is impossible to emit code for](#you-should-have-some-class-initialization-which-is-impossible-to-emit-code-for)
|
61 | 60 | - [External Tools](#external-tools)
|
62 | 61 | - [How do I write unit tests with TypeScript?](#how-do-i-write-unit-tests-with-typescript)
|
@@ -1214,39 +1213,6 @@ Second, the runtime characteristics of this class are very surprising.
|
1214 | 1213 | Instead of allocating one closure per method, this allocates one closure per method *per instance*.
|
1215 | 1214 | This expensive in terms of class initialization cost, memory pressure, and GC performance.
|
1216 | 1215 |
|
1217 |
| -### Why can't I access arbitrary properties on a type with a string indexer? |
1218 |
| -
|
1219 |
| -> When I declare an interface with a string indexer, |
1220 |
| -> I want to be able to access arbitrary properties on it, like this: |
1221 |
| -> ```ts |
1222 |
| -> interface StringMap { |
1223 |
| -> [index: string]: string; |
1224 |
| -> } |
1225 |
| -> function f(obj: StringMap) { |
1226 |
| -> obj.foo; // error! |
1227 |
| -> obj['foo']; // I have to write it this way |
1228 |
| -> } |
1229 |
| -> ``` |
1230 |
| -> Instead I can only use index syntax: `obj['foo']`. |
1231 |
| -
|
1232 |
| -The point of TypeScript is to catch errors at compile-time, and "Property does not exist" is one of the most important. |
1233 |
| -If a string indexer let you access properties on a type, you'd never get this error for those types. |
1234 |
| -This is important if you have other properties on the indexed type: |
1235 |
| -
|
1236 |
| -```ts |
1237 |
| -interface StringMap { |
1238 |
| - property1: string; |
1239 |
| - [index: string]: string; |
1240 |
| -} |
1241 |
| -function f(obj: StringMap) { |
1242 |
| - obj.property1; // ok |
1243 |
| - obj.propertyl; // error! |
1244 |
| - obj['foo']; // ok |
1245 |
| -} |
1246 |
| -``` |
1247 |
| -
|
1248 |
| -So TypeScript always checks property accesses and reserves arbitrary access for the indexer syntax. |
1249 |
| -
|
1250 | 1216 | ### You should have some class initialization which is impossible to emit code for
|
1251 | 1217 | TODO: Port content from [#1617](https://github.com/Microsoft/TypeScript/issues/1617)
|
1252 | 1218 |
|
|
0 commit comments