ArkType, the runtime validation library known for type-safe syntax, has introduced ArkRegex, a drop-in replacement for JavaScript’s RegExp constructor that brings full type inference to regular expressions.

ArkRegex introduces type safety to regular expressions without any runtime overhead, shipping with support for all native RegExp features including positional and named capture groups, flags, and pattern validation at the type level.

The library addresses a long-standing pain point in TypeScript development where regular expressions constructed from strings offer no type safety until runtime. ArkRegex infers string types directly from regex patterns, catching syntax errors like referencing non-existent groups as type errors rather than runtime failures.

The syntax for ArkRegex mirrors the native RegExp constructor, making adoption straightforward. An example from the documentation shows type inference for a simple pattern:


const ok = regex(“^ok$”, “i”)
// Regex<“ok” | “oK” | “Ok” | “OK”, { flags: “i” }>

For developers working with capture groups, ArkRegex provides automatic typing for both positional and named captures.

Named capture groups receive similar treatment, with the library inferring precise types for each named group in the pattern. This feature particularly benefits email validation and URL parsing patterns common in production applications.

The release performs best with TypeScript 5.9 or later, leveraging recent improvements to TypeScript’s type inference engine. Installation requires only running pnpm install arkregex or the equivalent command for other package managers.

ArkRegex joins existing solutions in the TypeScript regex ecosystem, notably magic-regexp which takes a different approach with a natural language syntax that compiles away to pure RegExp. Where magic-regexp focuses on readability through its builder pattern, ArkRegex prioritises familiarity by maintaining standard regex syntax whilst adding type inference. Developers comfortable with traditional regular expressions may find ArkRegex’s learning curve shallower, while those preferring explicit APIs might favour magic-regexp’s approach.

The library has generated positive reception from the TypeScript community. John De Goes remarked Insane. (In a good way.) in response to the announcement.

Discussion on Reddit’s TypeScript community brought a lot of praise for the project, with one commenter expressing trust of the author to deliver to the complexity required:

It’s absolutely obvious that one CAN do this on a type level. Would I ever attempt it, knowing how many edge cases and features this needs to support? Hell no. But knowing WHO implemented it, I have 100% trust that this just works as expected…

For complex expressions that exceed TypeScript’s type inference limits, ArkRegex provides the regex.as escape hatch allowing manual type annotations:


const complexPattern = [regex.as]()<`pattern-${string}`, {captures: [string]}>
(“very-long-complex-expression-here”)

The project is tested and benchmarked using attest, ArkType’s testing framework. The test suite covers edge cases across the regex syntax supported by the ECMAScript specification.

As ArkRegex is part of the broader ArkType project rather than a separate versioned package, migration considerations primarily apply to developers adopting the tool for the first time. The library requires no changes to existing regex patterns, functioning as a direct replacement for new RegExp() calls. The ArkType Visual Studio Code extension adds syntax highlighting for regex calls, improving the development experience.

ArkType is an open-source runtime validation library that parses optimised validators from type-safe syntax. The project extends TypeScript’s type system to runtime validation, enabling developers to define schemas that work both at compile time and runtime.