Skip to content

Typesafe regexp patterns#1411

Closed
timvandam wants to merge 3 commits intoarktypeio:mainfrom
timvandam:main
Closed

Typesafe regexp patterns#1411
timvandam wants to merge 3 commits intoarktypeio:mainfrom
timvandam:main

Conversation

@timvandam
Copy link

@timvandam timvandam commented Apr 9, 2025

This PR introduces a regexp pattern parser, which improves the type safety of regexp literals.

For example:

const UserId = type("/^user_[^_]$/#UserId") // Type<Brand<`user_${string}`, "UserId">, {}>
const UserAccountId = type("/^user_account_[^_]$/#UserAccountId") // Type<Brand<`user_account_${string}`, "UserAccountId">, {}>
const PostId = type("/^post_[^_]$/#PostId") // Type<Brand<`post_${string}`, "PostId">, {}>

Regexp patterns can be used in many places, so downstream type safety also improves as a result of this PR. For instance, the following test case:

const t = type({
	"[/^f/]": {
		a: "1"
	},
	"[/f$/]": { b: "1" },
	foof: { c: "1" }
})

const a = t.get("foo")
// Previously:
attest<{ a: 1 } | { b: 1 }>(a.infer)
// Now:
attest<{ a: 1 }>(a.infer)

const b = t.get("oof")
// Previously:
attest<{ a: 1 } | { b: 1 }>(b.infer)
// Now:
attest<{ b: 1 }>(b.infer)

The following tasks still need to be wrapped up:

  • Fix test cases
  • Fix error handling (either<a,b> does not seem efficient - use something like StaticState?)
  • Fix excessively deep type instantiation (introduced after merging with upstream + adding error handling - hopefully error handling is all that needs to change here)
  • Add negative test cases to assert the correct errors are raised

@github-project-automation github-project-automation bot moved this to To do in arktypeio Apr 9, 2025
@ssalbdivad ssalbdivad moved this from To do to In progress in arktypeio Apr 15, 2025
@ssalbdivad ssalbdivad moved this from In progress to Implemented in arktypeio Oct 28, 2025
@ssalbdivad
Copy link
Member

Thanks again so much for your work on this! I've integrated some of these ideas into the new arkregex package that is about to be published ❤️

@ssalbdivad ssalbdivad closed this Oct 28, 2025
@github-project-automation github-project-automation bot moved this from Implemented to Done (merged or closed) in arktypeio Oct 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done (merged or closed)

Development

Successfully merging this pull request may close these issues.

2 participants