Problem Statement
Create a template-literal route type like `/users/${number}/posts/${number}`; add a typed params parser.
Explanation
Use template literal types to model the exact string shape and then parse it with a small runtime function that returns strongly typed params. This catches route mistakes at compile time and gives you typed IDs at runtime.
Code Solution
SolutionRead Only
type UserPostsRoute = `/users/${number}/posts/${number}`;
function parseUserPosts(r: UserPostsRoute){
const m = r.match(/^\/users\/(\d+)\/posts\/(\d+)$/);
if (!m) throw new Error('bad route');
return { userId: Number(m[1]), postId: Number(m[2]) } as const;
}
const p = parseUserPosts('/users/12/posts/99'); // { userId: 12, postId: 99 }