Skip to content

fix(regex): include the empty string when a zero-min quantifier applies to a number pattern#1628

Open
spokodev wants to merge 1 commit into
arktypeio:mainfrom
spokodev:fix/arkregex-zero-quantifier-capture
Open

fix(regex): include the empty string when a zero-min quantifier applies to a number pattern#1628
spokodev wants to merge 1 commit into
arktypeio:mainfrom
spokodev:fix/arkregex-zero-quantifier-capture

Conversation

@spokodev

Copy link
Copy Markdown

Fixes #1625

Problem

regex("(\\d*)") types the capture group as `${number}`, which excludes the empty string even though * matches zero repetitions. ? has the same problem (regex("(\\d?)")`${number}`).

The tryFastPath branch in ark/regex/quantify.ts returns early for number-like patterns on the premise that repeating `${number}` does not change the type — true for one or more repetitions, but zero repetitions produce "", which `${number}` does not include. The early return runs before the min extends 0 handling, so every zero-min quantifier (*, ?, {0,n}) over a number pattern drops the empty branch. The string fast path is unaffected because string already contains "".

Fix

Make the number fast path min-aware: zero-min quantifiers produce "" | ${number}`` and quantifiers withmin >= 1keep the existing ``${number}` `` result.

Tests

Two type-level tests (* and ? over \d in a capture group) fail on main with

+ 'Regex<`${number}`, { captures: [`${number}`]; }>'
- 'Regex<`${number}` | "", { captures: [`${number}` | ""]; }>'

and pass with this change. Full mocha run: 1766 passing; the 2 failures in ark/attest/__tests__/snapPopulation.test.ts reproduce identically on a clean checkout of main and are unrelated. Prettier and ESLint clean on the changed files.

@github-project-automation github-project-automation Bot moved this to To do in arktypeio Jun 12, 2026
@spokodev spokodev marked this pull request as ready for review June 12, 2026 18:15

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ No new issues found.

Reviewed changes — fix for regex zero-min quantifier incorrectly narrowing number patterns to exclude the empty string.

  • Make tryFastPath in quantify.ts min-aware for number patterns — when a zero-min quantifier (*, ?, {0,n}) applies to a `${number}` pattern, the result now includes "" | `${number}` instead of just `${number}`.
  • Add type-level tests for * and ? over \d in a capture group.

Pullfrog  | View workflow run | Using DeepSeek Pro (free via Pullfrog for OSS) | 𝕏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: To do

Development

Successfully merging this pull request may close these issues.

Capture group mistyped with * quantifier

1 participant