Skip to content

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: In progress

Development

Successfully merging this pull request may close these issues.

1 participant