From typescriptlang.org:
TypeScript is a typed superset of JavaScript that compiles to plain JavaScript.
This is false.
Here, I have the error:
An index expression argument must be of type 'string', 'number', 'symbol', or 'any'
This is caused by the [ inline-map ] expression:
test = { true: 'Y', false: 'N', undefined: 'IDK', null: 'Nah', 0: 'none' }[ true ];
Isn't a Boolean
a type "<any>
"??? Pretty infuriating, since I use this syntax left right & center.
Moreover, the following works just fine:
{ true: 'Y', false: 'N', undefined: 'IDK', null: 'Nah', 0: 'none' }[ undefined ];
{ true: 'Y', false: 'N', undefined: 'IDK', null: 'Nah', 0: 'none' }[ null ];
The colloquial, "Anything that's legal JavaScript is perfectly legal TypeScript" is outright a false statement.
Obviously, I can write it as:
{ true: 'Y', false: 'N', undefined: 'IDK', null: 'Nah', 0: 'none' }[ ''+true ];
But why is Boolean
not of type any
???
Is this a bug of TypeScript?
Has this been reconciled in TS 2.1?
Can I correct this issue in tsconfig.json
or another way?