Proposal: LiteralInt
#1665
elenakrittik
started this conversation in
General
Replies: 3 comments 9 replies
|
I agree it seems like a good idea but I think this'd be much better generalised by Literal[int] working that way you get the other types that work with Literal for free |
8 replies
|
This would make it possible to provide correct typeshed annotations for |
1 reply
|
Another great use case would be for annotating Currently, it seems one has to manually add |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Similar to the
LiteralStringtype,LiteralIntis an integer that is (or is composed of) constant integer literals.The problem
Identificators (IDs) in many applications are represented as integers, and it is convenient from time to time to just hardcode an entity's ID that you know will not change when the API you use doesn't provide good means to programmatically find the necessary entity. This is the exact use case i have: i want all IDs in a library to be strongly typed and differentiated (so that you can't accidentally misuse, e.g., a user ID when a message ID was expected, as they will be different NewTypes of
int), but this creates a limitation: i can no longer pass rawintliteral where i used to do so, so i'll need to find all usages of that pattern and update them, which is prohibitive given the amount of code that already depends on this type-checking properly. I can't use something likeUserID | inteither because that will essentially eliminate the whole point and silently cast NewTypes of other ID types tointand therefore pass the type check.LiteralIntwould solve this.I'd like to hear others' opinion on this: is it worth it? Maybe some other use cases you have? Any feedback appreciated.
All reactions