Proposal: LiteralInt
#1665
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 |
Beta Was this translation helpful? Give feedback.
-
This would make it possible to provide correct typeshed annotations for |
Beta Was this translation helpful? Give feedback.
-
Another great use case would be for annotating Currently, it seems one has to manually add |
Beta Was this translation helpful? Give feedback.
-
Similar to the
LiteralString
type,LiteralInt
is 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 rawint
literal 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 | int
either because that will essentially eliminate the whole point and silently cast NewTypes of other ID types toint
and therefore pass the type check.LiteralInt
would 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.
Beta Was this translation helpful? Give feedback.
All reactions