Skip to content

Instantly share code, notes, and snippets.

@flisboac
Created September 22, 2021 21:45
Show Gist options
  • Save flisboac/e568ada339bc287b124197bceaedb273 to your computer and use it in GitHub Desktop.
Save flisboac/e568ada339bc287b124197bceaedb273 to your computer and use it in GitHub Desktop.
static-convert-path.ts
type ToPathIndexArray<S extends string> =
S extends `${infer N}[${infer P}]` ? [N, P] : [S];
type ToPathArray<P extends string, _R extends string[] = []> =
P extends `${infer NS}.${infer N}`
? NS extends `${infer IN}[${infer I}]`
? [IN, I, ...ToPathArray<N>, ..._R]
: [...ToPathIndexArray<NS>, ...ToPathArray<N>, ..._R]
: P extends `${infer IN}[${infer I}]`
? [IN, I, ..._R]
: P extends '['
? never
: [P, ..._R]
type T3 = ToPathIndexArray<'a'>
type T2 = ToPathArray<'a[1].x.b[1]'>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment