0

when using let s = Symbol() to declare the symbol as key of an object, such as:

let a = { [s]:1 }

the type of a will automatically become {[x:string]:number} whereas it will get error when try to index like a[s]:

Type 'symbol' cannot be used as an index type (2538)

while use const s = Symbol() declaration is work as become to {[s]:number}.

db9035
  • 131
  • 1
  • 5
  • 1
    Probably [this](https://github.com/Microsoft/TypeScript/issues/24587). – Amadan Dec 10 '19 at 09:29
  • 1
    See also https://stackoverflow.com/questions/59118271/using-symbol-as-object-key-type-in-typescript – Alexey Romanov Dec 10 '19 at 09:41
  • 1
    Do you want `a` to have *all* symbols as its keys, or just the one specified by `s`? If the former, I don't think that's possible. If the latter, then `const` works because the compiler knows its value won't change so it is inferred more narrowly, whereas `let` allows it to change so it is inferred as just `symbol`, which leads to the problem. – jcalz Dec 10 '19 at 15:30
  • @jcalz it's latter, thanks. and just curious about the `symbol->key:string` transform behavior – db9035 Dec 10 '19 at 23:55