Suggestion: Range as Number type
See original GitHub issueWhen defining a type one can specify multiple numbers separated by |
.
type TTerminalColors = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15;
Allow to specify number types as ranges, instead of listing each number:
type TTerminalColors = 0..15;
type TRgbColorComponent = 0..255;
type TUInt = 0..4294967295;
Maybe use ..
for integers and ...
for floats.
interface Math {
random(): 0...1
}
type RandomDice = 1..6;
const roll: RandomDice = Math.floor(Math.random() * 6);
// Error: -------------------------^ Maybe use Math.ceil()?
Issue Analytics
- State:
- Created 6 years ago
- Reactions:1389
- Comments:145 (33 by maintainers)
Top Results From Across the Web
typescript - Is it possible to restrict number to a certain range
For smallish ranges, this answer works: type Enumerate<N extends number, Acc extends number[] = []> = Acc['length'] extends N ? Acc[number] ...
Read more >Typescript Numeric Range Type - DEV Community
Typescript will complain if your starting number is 1000 or your range size is 1000.
Read more ><input type="range"> - HTML: HyperText Markup Language
<input> elements of type range let the user specify a numeric value which must be no less than a given value, and no...
Read more >HTML input type="range" - W3Schools
Default range is 0 to 100. However, you can set restrictions on what numbers are accepted with the attributes below. max - specifies...
Read more >How to Select the Right Data Types | Tutorial by Chartio
Data types define what kind and range of data can be stored in a given field (or column). ... The kind of data...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
This idea can be expanded to characters, e.g.
"b".."d"
would be"b" | "c" | "d"
. It would be easier to specify character sets.@aluanhaddad actually it would be far from infinite in IEEE floating point. It would have 1,065,353,217 inhabitants by my calculations.