Skip to content

Instantly share code, notes, and snippets.

@Grubba27
Created January 30, 2023 21:09
Show Gist options
  • Save Grubba27/0ddac06be68b7c637d0c7eb266f0565a to your computer and use it in GitHub Desktop.
Save Grubba27/0ddac06be68b7c637d0c7eb266f0565a to your computer and use it in GitHub Desktop.
Get url params
type GetParams<
Text extends string,
Result extends string = ''
> =
Text extends `${ infer L }${ infer R }`
? L extends '/'
? Result
: GetParams<R, `${ Result }${ L }`>
: Result
type GetURLParams<URL extends string, Params = {}, > =
URL extends `${ infer L }${ infer R }`
? L extends ':'
? GetURLParams<R, { [p in GetParams<R>]: string } & Params>
: GetURLParams<R, Params>
: Params
let url: GetURLParams<'/:id/:d2/'> = {d2: '', id: ''}
let url: GetURLParams<'/:other/:d2/'>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment