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?
Booleanandanyare different types.Booleanis compatible withany, but it does not saycompatible with type string, ... or any, it saysmust be of type .... Also, errors reported by typescript do not prevent it from compiling your source code to javascript (unless you set--noEmitOnErrorcompiler option explicitly), so yes, it is a superset of JavaScript.obj[<any>bool]work? in general, claims made by frameworks are often exaggerated...is arelationship is not very meaningful for structural types (all types in typescript except enums are structural).is ais one-way only and transitive (A is a B and B is a C means A is a C). Structural typing is completely different beast, it's modeled withA is assignable to Brelation, andanywas deliberately made 2-way compatible with any type, so you have weird thing whereintis assignable toanyandanyis assignable tostringbut notintassignable tostring