Home > Software engineering >  Infer expression from a string in Typescript
Infer expression from a string in Typescript

Time:11-15

Given the code below, everything seems ok (see playground):

type PathParam<T> = T extends `${string}:${infer U}` ? U : never;

type Param = PathParam<"/post/:post_id">;
// type Param = "post_id"

But it gets a little bit complicated when there's additional text after the path param.

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 can understand why it fails, but I'm not sure how can infer only the pattern :${string} in typescript. In regex it would be equivalent to /:[a-z_] /g

So my question is - how can I make Param to be inferred as the following type?

type Param = "post_id" | "like_id"

CodePudding user response:

You need recursive type to perform some loop to construct dynamic sized union.

try:

type PathParam<T extends string, Res extends string = never> = 
    T extends `${string}:${infer U}/${infer Rest}` 
    ?  U | PathParam<Rest, Res> :
    T extends `${string}:${infer L}` 
    ?  L : Res;
  • Related