TypeScript
TypeScript copied to clipboard
Allow bigint as an index type because number is allowed too
Suggestion
The bigint type currently is not allowed as an index type requiring an explicit string cast.
const n = 100n;
const obj = {};
obj[n] = 1;
In JS, it's automatically cast as a string. This is what happens with numbers too. If numbers are automatically cast to strings, then why not allow bigint to be indexable as well? It would avoid having to explicitly cast all the time when it's not necessary in JS.
🔍 Search Terms
bigint index
✅ Viability Checklist
My suggestion meets these guidelines:
- [x] This wouldn't be a breaking change in existing TypeScript/JavaScript code
- [x] This wouldn't change the runtime behavior of existing JavaScript code
- [x] This could be implemented without emitting different JS based on the types of the expressions
- [x] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
- [x] This feature would agree with the rest of TypeScript's Design Goals.
⭐ Suggestion
Just make bigint indexable.
📃 Motivating Example
const n = 100n;
const obj = {};
obj[n] = 1;
This is IMO a pretty reasonable suggestion that would smooth out some use cases, like going from reasonable bit masks that fit within the safe range of the number type to very large bitmasks that require bigints to work, without any drawbacks (that I can think of anyway).
Maybe a somewhat compelling use case: I have like hundreds of errors because bigints are not treated like numbers in records here: https://github.com/fabiospampinato/shosho/blob/7d688cf74a1a44aca7454c54902a606974f86053/src/maps.ts#L300-L817
Wrapping all of that in String() would be kinda weird, so I guess I'll have the errors 🤷
The fact that #19139 isn't available either makes this extra frustrating.
👀 Microsoft deleted a comment from me 15 hrs ago - I wasnt even aware I commented this issue ever.
What's going on? Anyone happen to have a screencap for me to investigate? Got no suspicious activity according to the GH settings page nor idle sessions etc.
Your comment was simply noting what year it is. We expect comments to meaningfully contribute to the discussion and will clean up comments that don't do that. See also
Huh... oh well.. I guess that's reasonable for deletion. And here I was fearing the worst, some spamlinks or similar.
Thanks for clarification!