TypeScript icon indicating copy to clipboard operation
TypeScript copied to clipboard

Allow bigint as an index type because number is allowed too

Open CMCDragonkai opened this issue 4 years ago • 7 comments

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;

CMCDragonkai avatar Oct 16 '21 08:10 CMCDragonkai

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).

fabiospampinato avatar Jun 04 '23 18:06 fabiospampinato

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 🤷

fabiospampinato avatar Jun 09 '23 11:06 fabiospampinato

The fact that #19139 isn't available either makes this extra frustrating.

mhuggins avatar Apr 29 '24 14:04 mhuggins

👀 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.

f0o avatar Apr 30 '24 06:04 f0o

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

RyanCavanaugh avatar May 01 '24 16:05 RyanCavanaugh

Huh... oh well.. I guess that's reasonable for deletion. And here I was fearing the worst, some spamlinks or similar.

Thanks for clarification!

f0o avatar May 02 '24 11:05 f0o