Reputation: 21
I have this line of code:
type RouterQuery = keyof AppRouter['_def']['queries'];
Which will give me this type:
type RouterQuery = "healthz" | "post.all" | "post.byId" | "category.all" | "category.byId" | "course.all" | "course.featured"
I'm trying to get only strings that have an '.all' postfix.
The desired type would be:
type RouterQueryAll = "post.all" | "category.all" | "course.all"
How can I do that?
Upvotes: 2
Views: 736
Reputation: 14118
type RouterQueryAll = Extract<RouterQuery, `${string}.all`>
Using Extract
, you can ‘filter’/extract types from a union that are assignable to another type. `${string}.all`
is an example of a template literal type that matches all strings ending in .all
.
Upvotes: 6