Registry / serialization / type-level-regexp

type-level-regexp

JSON →
library0.1.17jsnpmunverified

Type-Level RegExp (current version 0.1.17) is a TypeScript library that implements a regular expression parser and matcher entirely at the type level, leveraging TypeScript's template literal types. It allows developers to define regex patterns as string literals and receive compile-time type-safe results when using `String.match()`, `String.matchAll()`, and `String.replace()`. This provides robust validation of regex syntax and strongly typed access to capture groups, significantly reducing runtime errors related to incorrect regex patterns or mismatched group access. The library is currently under active development ('Work In Progress'), implying an iterative release cadence focused on feature expansion and refinement. Its primary differentiator is the deep integration of regex parsing into the TypeScript type system, offering static analysis benefits not typically found in runtime-only regex solutions.

npm install type-level-regexp
INSTALL
IMPORT
SIG · TYPE-LEVEL-REGEXP
T
type-level-regexp
serializationjavascriptv0.1.17
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

createRegExp
import { createRegExp } from 'type-level-regexp'
const createRegExp = require('type-level-regexp')
Primary function to create a typed RegExp object. Uses ESM imports.
spreadRegExpIterator
import { spreadRegExpIterator } from 'type-level-regexp'
const spreadRegExpIterator = require('type-level-regexp').spreadRegExpIterator
Utility function for converting the iterator returned by `String.matchAll()` into a typed array.
ParseRegExp
import { ParseRegExp } from 'type-level-regexp'
Type-level utility for parsing a RegExp string into its abstract syntax tree (AST). Imported as a type, not a runtime value.
MatchRegExp
import { MatchRegExp } from 'type-level-regexp'
Type-level utility for matching a string against a parsed RegExp AST. Imported as a type, not a runtime value.

Demonstrates how to use `createRegExp` to get strongly typed match results for `String.match()`, `String.replace()`, and `String.matchAll()` against literal strings, including access to capture groups and iterator spreading.

import { createRegExp, spreadRegExpIterator } from 'type-level-regexp' /** string.match() */ const regExp = createRegExp('foO(?<g1>b[a-g]r)(?:BAz|(?<g2>qux))', ['i']) const matchResult = 'prefix foobarbaz suffix'.match(regExp) // matching literal string if (matchResult) { console.log(matchResult[0]) // 'foobarbaz' console.log(matchResult[1]) // 'bar' // matchResult[3] // TypeScript error: type '3' can't be used to index type 'RegExpMatchResult<...>' (as shown in README) console.log(matchResult.length) // 3 console.log(matchResult.index) // 7 console.log(matchResult.groups) // { g1: "bar"; g2: undefined; } } /** string.replace() */ const regExp2 = createRegExp('(\\d{4})[-.](?<month>\\w{3,4})[-.](?:\\d{1,2})') const replaceResult = '1991-Sept-15'.replace(regExp2, '$<month> $3, $1') console.log(replaceResult) // 'Sept 15, 1991' /** string.matchAll() */ const regExp3 = createRegExp('c[a-z]{2}', ['g']) const matchALlIterator = 'cat car caw cay caw cay'.matchAll(regExp3) const spreadedResult = spreadRegExpIterator(matchALlIterator) console.log(spreadedResult[2][0]) // 'caw' console.log(spreadedResult[3]?.index) // 12
Debug
Known issues
gotchaThe library is explicitly marked as 'Work In Progress' (WIP), meaning that APIs and internal implementations are subject to change without strict adherence to semantic versioning for minor updates. Users should expect potential breaking changes in minor or patch releases until a stable major version is released.
fix
Monitor the GitHub repository for updates and review release notes carefully before upgrading. Pin exact versions for production use and consider using the TypeScript Playground link to test changes.
affects: >=0.1.0
gotchaWhen matching against dynamic (non-literal) strings, the type-level matcher will provide enumerated or less precise types for match results, as the exact match structure cannot be determined at compile-time. Full type-level precision is primarily available when matching against string literals.
fix
For dynamic string matching, validate runtime results if strict typing is critical, or consider using runtime assertions. If possible, refactor to use literal strings where type-level guarantees are most valuable.
affects: >=0.1.0
breakingSupplying an invalid regular expression pattern to `createRegExp` will result in a TypeScript compilation error, preventing the code from compiling. This is a deliberate design choice to enforce regex correctness at compile-time, unlike runtime regex engines that would throw a runtime error.
fix
Ensure all regular expression patterns passed to `createRegExp` are syntactically valid and correctly formed according to JavaScript RegExp syntax. Utilize online regex testers or the provided TypeScript Playground link for validation during development.
affects: >=0.1.0
Errors
Common errors & fixes
Argument of type 'string' is not assignable to parameter of type 'RegExpSyntaxError<"Invalid regular expression, missing closing `)`">'
An invalid regular expression string was passed to the `createRegExp` function.
fix
Correct the regular expression string to be syntactically valid (e.g., ensure all opening parentheses have a matching closing parenthesis).
Upgrade
Version history
0.1.17latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
23 hits · last 30 days
node
18
OpenAI (training)
1
Resources
type-level-regexp — npm install type-level-regexp · libregistry