Skip to content

Commit 9beebb6

Browse files
authored
Remove outdated & incorrect section on string indexers
As of [version 2.2](https://devblogs.microsoft.com/typescript/announcing-typescript-2-2/), it is no longer accurate.
1 parent c3a13cf commit 9beebb6

File tree

1 file changed

+0
-34
lines changed

1 file changed

+0
-34
lines changed

FAQ.md

-34
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@
5656
- [Things That Don't Work](#things-that-dont-work)
5757
- [You should emit classes like this so they have real private members](#you-should-emit-classes-like-this-so-they-have-real-private-members)
5858
- [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)
6059
- [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)
6160
- [External Tools](#external-tools)
6261
- [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.
12141213
Instead of allocating one closure per method, this allocates one closure per method *per instance*.
12151214
This expensive in terms of class initialization cost, memory pressure, and GC performance.
12161215
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-
12501216
### You should have some class initialization which is impossible to emit code for
12511217
TODO: Port content from [#1617](https://github.com/Microsoft/TypeScript/issues/1617)
12521218

0 commit comments

Comments
 (0)