Skip to content
webtype.orgwebtype.org#233 starts-with · par 3

    ↑↓ move · ⏎ open · esc close

    No. 233 · August 16, 2026 · Moderate

    StartsWith

    Implement `StartsWith<S, P>` so it reports whether the string `S` begins with the prefix `P`. Beginning with, not merely containing.

    01

    Try the puzzle yourself

    Par 3

    Puzzle

    starts-with.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
    • StartsWith<'hello world', 'hello'>
      true
    • StartsWith<'hello world', 'world'>
      false
    • StartsWith<'abc', ''>
      true
    • StartsWith<'', 'a'>
      false

    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 17, 2026

    The solution

    type StartsWith<S extends string, P extends string> =
      S extends `${P}${string}` ? true : false

    The common wrong answer

    type StartsWith<S extends string, P extends string> =
      S extends `${string}${P}${string}` ? true : false

    This asks whether `P` appears *anywhere* — it is `includes`, not `startsWith`. The leading `${string}` lets the match slide past the beginning, so `"hello world"` reports `true` for the prefix `"world"`.

    Line by line

    1. `${P}${string}`

      A template literal pattern is anchored at both ends. Putting `P` first means the very first characters of `S` must be `P`; the trailing `${string}` then accepts any remainder, including none at all.

    2. StartsWith<'abc', ''>

      The empty prefix collapses the pattern to `${string}`, which every string satisfies — so the answer is `true`, matching how `"abc".startsWith("")` behaves at runtime.

    Takeaway

    Where you put `${string}` in a pattern is where you allow slack. Leading slack means "contains", trailing slack means "starts with", and slack on both sides means you have written a substring test by accident.

    Uses