Annotated solution
Published August 27, 2026The solution
type Absolute<N extends number> = `${N}` extends `-${infer R}` ? R : `${N}`
The common wrong answer
type Absolute<N extends number> = N extends `-${infer R}` ? R : `${N}`
The pattern is right but the subject is the wrong kind of thing. `-5` is a numeric literal type, and a numeric literal never matches a template literal pattern, so the conditional is always false and every input falls through to `${N}` — returning `"-5"` unchanged.
Line by line
`${N}`Interpolating a number into an otherwise empty template literal is the type-level equivalent of `String(n)`. `-5` becomes the string literal type `"-5"`, which patterns can now match against.
`-${infer R}`The minus is matched literally and `R` binds everything after it. Positive numbers simply fail this pattern and take the false branch, so no separate sign test is needed.
Takeaway
The type level has no arithmetic, but it has text. Stringifying a number turns numeric problems into pattern-matching problems — which is how the standard trick for absolute value, digit counting and sign comparison all work.
