Looking at the code snippet below, everything appears to be in order (view playground):
type PathParam<T> = T extends `${string}:${infer U}` ? U : never;
type Param = PathParam<"/post/:post_id">;
// type Param = "post_id"
However, things become a bit more complex when there is additional text following the path parameter.
type PathParam<T> = T extends `${string}:${infer U}` ? U : never;
type Param = PathParam<"/post/:post_id/likes/:like_id">;
// type Param = "post_id/likes/:like_id"
I understand why it fails in this scenario, but I am unsure how to only infer the pattern :${string}
in typescript. In regex terms, it would be similar to /:[a-z_]+/g
Therefore, my inquiry is - how can I modify Param
to be inferred as the subsequent type?
type Param = "post_id" | "like_id"