Open
Description
Hi @mattpocock!!
First of all, thanks so much for these materials you put together. I don't think I've become a wizard yet, but going through the topics and exercises has been quite fun and I feel like I've learned a lot from it already!! 🤯 😍
I just finished watching Transform Path Parameters from Strings to Objects
. I didn't think to use the S.Split
helper, so I came up with a recursive solution that felt easier to reason with (for me that I wrote it, of course! 😂)
Just pasting it here in case you think something like that might help others in the future and curious to know how you think it compares with the proposed solution ❤️
import { Equal, Expect } from '../helpers/type-utils';
type UserPath = '/users/:id';
type UserOrganisationPath = '/users/:id/organisations/:organisationId';
type UserOrganisationTeamPath =
'/users/:id/organisations/:organisationId/teams/:teamId';
// Extracts param by param until the end
type ExtractParams<T> = T extends `${string}:${infer Param}/${infer RestParams}`
? Param | ExtractParams<RestParams>
: T extends `${string}:${infer Param}`
? Param
: never;
type ExtractPathParams<T> = {
[K in ExtractParams<T>]: string;
};
type tests = [
Expect<Equal<ExtractPathParams<UserPath>, { id: string }>>,
Expect<
Equal<
ExtractPathParams<UserOrganisationPath>,
{ id: string; organisationId: string }
>
>,
Expect<
Equal<
ExtractPathParams<UserOrganisationTeamPath>,
{ id: string; organisationId: string; teamId: string }
>
>
];
Metadata
Metadata
Assignees
Labels
No labels