"All keys in the square bracket notation are converted to strings, unless they're Symbols. JavaScript object property names (keys) can only bestrings or Symbols. … JavaScript will call the anotherObj.toString() method, and use this result string as the new key."
So TypeScript will also allow string | number as a Key type but the distinct values 10 and "10" will always map to the same key "10".
If it is necessary to use types other than string or Symbol as a Key consider using the Map type instead:
"A Map's keys can be any value (including functions, objects, or any primitive)."
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Even the TypeScript documentation is sloppy here.
Ultimately Records are always based on plain JavaScript objects:
"All keys in the square bracket notation are converted to strings, unless they're Symbols. JavaScript object property names (keys) can only be strings or Symbols. … JavaScript will call the anotherObj.toString() method, and use this result string as the new key."
So TypeScript will also allow
string | number
as aKey
type but the distinct values10
and"10"
will always map to the same key"10"
.If it is necessary to use types other than
string
orSymbol
as aKey
consider using theMap
type instead:"A
Map
's keys can be any value (including functions, objects, or any primitive)."