Summary
This is a cautionary tale about how I ended up writing a (bad) regular expression parser and evaluator in pure TypeScript types.
type HexStr<S extends string> = Recognize<"[0-9a-fA-F]+", S>;
type IsMatch<S extends string> = HexStr<S> extends S
? true
: false;
const isHex: IsMatch<"deadbeef"> = true
const notHex: IsMatch<"nope"> = false
The novelty here is parsing and evaluating regular expressions at compile-time. The inspiration for this technique came from a comment from 2020 on the RegEx-validated string type discussion.
Backstory
To prepare for Halloween, I was writing a function castSpell that accepts a valid hex string.
I was writing in TypeScript, so the usual way to do this is using branded types, also known as nominal types:

Comments
Nothing yet. Say the first thing.
Sign in to join the conversation.