Skip to content

is ArkRegex a drop-in replacement for new RegExp even with dynamic patterns? #1576

@alexbit-codemod

Description

@alexbit-codemod

Hi ArkRegex team 👋

First off, ArkRegex is a really impressive project. The idea of statically typed regexes is fantastic.

I’m opening this issue to clarify an assumption I have made, and to confirm whether the behavior I’m seeing is intended.


Background / expectation

From the docs and blog posts, I was under the impression that regex() is a drop-in replacement for new RegExp(), meaning:

  • literal patterns get strong type inference
  • dynamic patterns still work, but with degraded (widened) typing

In other words, I expected something like this to type-check (even if with less precise types):

const r = regex(dynamicPattern) // dynamicPattern: string

What I’m observing instead

In practice, when the pattern is dynamic (string), the call often becomes ill-typed, with the parameter inferred as never, for example:

(alias) regex<string, "">(src: never, flags?: "" | undefined): never

This effectively makes regex() unusable without regex.as for dynamic patterns, even though the equivalent new RegExp(dynamicPattern) is valid.

Concrete example from a real migration attempt:

https://github.com/alexbit-codemod/netlify-cli/pull/5/files#r2660169105


Clarifying question

Is the following statement correct?

ArkRegex’s regex() function only accepts statically analyzable (literal) string patterns at the type level; dynamic patterns require regex.as.

If this is correct, then:

  • regex() is not a drop-in replacement for new RegExp() at the type level
  • dynamic patterns must always be rewritten to regex.as<...>(...) in order to compile

If this is not correct:

  • is there a recommended way to use regex() with dynamic string patterns without immediately resorting to regex.as?

Why this matters

I’m building a codemod to migrate new RegExp(...) to ArkRegex, and this distinction affects whether:

  • dynamic cases can be rewritten mechanically, or
  • must be skipped or rewritten with regex.as plus TODOs

Thanks in advance for the clarification. 🙏

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Status

    To do

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions