Annotated solution
Published August 17, 2026The 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
`${P}${string}`A template literal pattern is anchored at both ends. Putting
Pfirst means the very first characters ofSmust beP; the trailing${string}then accepts any remainder, including none at all.StartsWith<'abc', ''>The empty prefix collapses the pattern to
${string}, which every string satisfies — so the answer istrue, 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.

