Skip to content
#243 absolute-value · par 3

    ↑↓ move · ⏎ open · esc close

    No. 243 · August 26, 2026 · Moderate

    Absolute

    Implement `Absolute<N>` so it returns the magnitude of a number literal as a **string**. There is no negation at the type level — you have to remove the sign as text.

    01

    Try the puzzle yourself

    Par 3

    Puzzle

    absolute-value.ts
    Stroke 1 of 3Not run yet

    Replace ??? — your solution is checked against the cases below. Tab indents; press Escape then Tab to move focus out.

    Checks

    4
    • Absolute<-5>
      '5'
    • Absolute<5>
      '5'
    • Absolute<0>
      '0'
    • Absolute<-100>
      '100'

    How a check is judged Exact type equality, not assignability — an intersection is not the same as the flattened object.

    How everyone did

    Fewer than 5 people have solved this one so far. The distribution appears once there is enough of a sample to mean anything.

    Short game

    Fewest characters

    No public scores yet.

    Archive
    02

    Annotated solution

    Published August 27, 2026

    The 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

    1. `${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.

    2. `-${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.

    Uses