Skip to content

Instantly share code, notes, and snippets.

@shmidt-i
Created January 16, 2019 09:02
Show Gist options
  • Save shmidt-i/fc0ed919e5fe421d3cea67b594821153 to your computer and use it in GitHub Desktop.
Save shmidt-i/fc0ed919e5fe421d3cea67b594821153 to your computer and use it in GitHub Desktop.
Tail type and Semi-path type
type Tail<T extends any[]> = ((...args: T) => any) extends ((
_: infer First,
...rest: infer Rest
) => any)
? T extends any[] ? Rest : ReadonlyArray<Rest[number]>
: []
type GrabDeepType<T extends Record<any, any>, PathType> = {
'deeper': PathType extends [infer HeadType, ...any[]]
? HeadType extends undefined
? never
: GrabDeepType<T[PathType[0]], Tail<PathType>>
: never,
'end': T
}[
PathType extends [infer HeadType, ...any[]]
? HeadType extends undefined
? 'end'
: 'deeper'
:'end'
]
const obj = {
some: {
deep: {
value: true as true,
other: 'hello' as 'hello'
}
}
}
// Can't use it for now for ramda's path definition since array type widening TS 3.2
var aa: GrabDeepType<typeof obj, ['some', 'deep', 'value']>;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment