Exact regex types for digit patterns without combinatorial explosion (`\d{4}`, `\d+`, `0|[1-9]\d*`)
Request a feature
Motivation
The ArkRegex FAQ explains that ranges like [a-Z] aren't inferred precisely because building literal types for them "is combinatorial and will explode very quickly". Digit patterns are currently widened too: /^\d{4}$/ infers ${number}, which also accepts "12345", " " and "1e5".
For digits, this doesn't have to be a trade-off. Most digit patterns can be typed exactly, and the rest much more tightly, using types whose size doesn't grow with the pattern (no unions that multiply per character). I fuzzed the encodings below against real RegExp results: 4 seeds, 10,927 real matches (digit runs up to 1,000+ characters), on TS 5.1, 5.2, 5.3, 5.4, 5.9, 7.0 and 7.1-dev. Zero real matches were rejected on any version.
Same 4,917 strings, TS 7.0 (5.9 gives identical numbers):
| Pattern | arkregex 0.0.10: matches wrongly rejected | proposed | arkregex: near-misses rejected | proposed |
|---|---|---|---|---|
^\d{4}$ |
0 | 0 | 288/576 | 576/576 |
^\d{6}$ |
0 | 0 | 292/589 | 589/589 |
^\d{4}-\d{2}-\d{2}$ |
0 | 0 | 307/589 | 589/589 |
^\d+$ |
26 | 0 | 113/138 | 124/138 |
^\d*$ |
16 | 0 | 113/141 | 132/141 |
^v\d+\.\d+$ |
25 | 0 | 72/77 | 70/77 |
| semver core | 9 | 0 | 89/115 | 115/115 |
The "wrongly rejected" column is the ${number} overflow bug from #1663. Near-misses are mutations of real matches: inserted spaces, e, x, -, _, non-ASCII digits, extra/missing digits.
Type-check time is about the same: 88 ms for the proposed types vs 75 ms for arkregex's on TS 7.0.
Solution
Building blocks (the only union is Digit, a fixed 10 members):
/** Only matches "". Unlike `""` or `"" & {}`, it isn't folded into the literal text,
* so it can pin the end of a pattern or stop a class from absorbing following text. (TS 5.4+) */
type Z = "" & String
/** Matches exactly one ASCII digit when directly followed by another placeholder. (TS 5.3+) */
type D = bigint & {}
type Caseless = Lowercase<string> & Uppercase<string>
type Digit = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
/** Exactly 0|[1-9]\d*: no sign, hex, exponent, separators or length limit. (TS 5.3+) */
type Int = `${bigint}` & Caseless & `${D}${string}`Encodings:
| Regex | Type |
|---|---|
\d{4} |
`${D}${D}${D}${D}${Z}` |
\d{2}-\d{2} |
`${D}${D}${Z}-${D}${D}${Z}` |
0|[1-9]\d* |
Int (exact) |
\d+ |
Int | (`0${string}` & Caseless & `${string}${Digit}`) (exact without a leading zero, loose with one) |
The Z after each run matters: without it, `${D}x` accepts "123x", because a placeholder followed by literal text absorbs everything up to that text.
Older compilers are handled by feature detection, falling back to types that are sound on every version:
type HasIntersectionPlaceholders = "12" extends `${D}${string}` ? true : false // TS 5.3+
type HasZeroWidth =
"12" extends `${D}${D}${Z}` ?
"123" extends `${D}${D}${Z}` ?
false
: true
: false // TS 5.4+HasIntersectionPlaceholders is false on 5.1–5.2 and HasZeroWidth is false on 5.1–5.3. In those cases the types fall back to loose ones that are still sound (e.g. the DigitsPlus from the bug report), so nothing is ever stricter than the regex.
This would presumably apply to regex literals in type("/.../") too, since they currently infer the same ${number} as regex().
Trade-offs
- Hover output is noisier:
`${bigint & {}}${bigint & {}}…`instead of`${number}`. \d+with a leading zero is still a (tighter) superset; I couldn't find an exact union-free encoding for it.- Per-character classes other than digits (
[0-9a-f],[a-z]) still can't be expressed without unions, so this only covers\d-based patterns.
Happy to put together a PR if this direction sounds good.
Source: arktypeio/arktype