#1664·arktype

用于数字模式的精确正则表达式类型,避免组合爆炸(`\d{4}`, `\d+`, `0|[1-9]\d*`)

作者: danfry1创建于 2026年9月27日更新于 2026年9月27日

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.

内容来源: arktypeio/arktype