Skip to content

Instantly share code, notes, and snippets.

@vvakame
Created February 12, 2015 05:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vvakame/a0eeb258b4c2cbfa5e16 to your computer and use it in GitHub Desktop.
Save vvakame/a0eeb258b4c2cbfa5e16 to your computer and use it in GitHub Desktop.
Enum["hoge"] とかやるとエラーになる件 http://twitter.com/fakebrainwasher/status/565728129743089665
$ tsc --version
message TS6029: Version 1.4.1.0

--noImplicitAny 有りだと、インデックスアクセスでanyになる場合怒られます。

$ tsc --noImplicitAny test.ts
test.ts(10,12): error TS7017: Index signature of object type implicitly has an 'any' type.
test.ts(13,12): error TS7017: Index signature of object type implicitly has an 'any' type.

TypeScript 1.4.1からは --suppressImplicitAnyIndexErrors というオプションが追加されました。

$ tsc --noImplicitAny --suppressImplicitAnyIndexErrors test.ts
$ echo $?
0

ベストプラクティス的には、インデクサアクセスをやめよう!となります。

enum Foo {
foo,
bar,
buzz,
}
// "foo" は存在するのでOK
var obj1 = Foo["foo"];
// "hoge" は存在しないのでNG
var obj2 = Foo["hoge"];
// 変数を使うとコンパイル時に確定しないのでNG
var str = "foo";
var obj3 = Foo[str];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment