Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow bigint as an index type because number is allowed too #46395

Open
5 tasks done
CMCDragonkai opened this issue Oct 16, 2021 · 7 comments
Open
5 tasks done

Allow bigint as an index type because number is allowed too #46395

CMCDragonkai opened this issue Oct 16, 2021 · 7 comments
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript

Comments

@CMCDragonkai
Copy link

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:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • 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;
@andrewbranch andrewbranch added Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript labels Nov 12, 2021
@fabiospampinato
Copy link

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
Copy link

fabiospampinato commented Jun 9, 2023

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 🤷

@mhuggins
Copy link

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

@microsoft microsoft deleted a comment from f0o Apr 29, 2024
@f0o
Copy link

f0o commented Apr 30, 2024

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

@RyanCavanaugh
Copy link
Member

RyanCavanaugh commented May 1, 2024

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

@f0o
Copy link

f0o commented May 2, 2024

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

Thanks for clarification!

@sethyuan
Copy link

3 years have passed and no advances? I got hundreds of errors due to this. Just why can't bigint be used as index type but number can? It causes no issues in JS.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

7 participants