-
-
Notifications
You must be signed in to change notification settings - Fork 567
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
Add numeric range types #319
Add numeric range types #319
Conversation
You need to add them to the readme. |
Zero is technically not a positive number, so maybe we should rename |
Would you be willing to add a |
These types are not just enforcing ranges though, but also types. Maybe it would be more correct to name the file |
Note to self:
|
Should we add a https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isFinite |
I'm also wondering whether we should be explicit in the types: |
source/numeric-range.d.ts
Outdated
*/ | ||
// `${bigint}` is a type that matches a valid bigint literal without the `n` (ex. 1, 0b1, 0o1, 0x1) | ||
// Because T is a number and not a string we can effectively use this to filter out any numbers containing decimal points | ||
export type Integer<T extends number> = `${T}` extends Infinities ? never : `${T}` extends `${bigint}` ? T : never; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder how this technique compares to https://stackoverflow.com/a/69413070/64949
source/numeric-range.d.ts
Outdated
A `number` that is an integer. | ||
|
||
Use-case: Validating and documenting parameters. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are there any open TS issues about adding an integer type that we could link to here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
microsoft/TypeScript#28682 seems to be the best fit
I have added
Renamed.
I have added a
I would advise against this as those types can accept a |
When do you ever need only a positive number though? I always want zero too. I also fear the type will be confusing to users as not everyone knows that a "positive" number does not include zero. |
Also, then the docs you have provided are confusing as you show the range |
I don't really see the usefulness of these. |
Also, the exact definition of "natural number" seems to be unclear:
|
Needs to be added to the readme. There are some other missing types in the readme too. |
I suggest we rename |
declare function setLength<T extends number>(length: Natural<T>): void; | ||
``` | ||
|
||
@category Utilities |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we maybe make a new Numeric
category?
I think there are some use cases for 1 or more vs 0 or more (mostly just enforcing a minimum value).
That's not correct, in range syntax a
I'm going to rename the types to have more useful names rather than follow strict mathematical definitions. It's clear that trying to apply those concepts to software ends up being unnecessarily confusing. |
Sure, but it's not a common case.
I don't think there's any common standard for this. Personally, I'm mostly familiar with range syntax from Swift which is inclusive: |
Fair enough, I have removed the |
Very nice work. Thanks for adding these 👍 |
This PR adds "numeric range" types,
number
/bigint
types for validating "ranges" of numbers (ex. integers, positive numbers)Inspired by the Nim programming language's
Natural
andPositive
range types.They can be useful for validating and documenting parameters.
For example, the
BigInt
function could be typed asand prevent the following code from being a runtime error:
Other examples are included in the doc comments.