Created
January 16, 2019 09:02
-
-
Save shmidt-i/fc0ed919e5fe421d3cea67b594821153 to your computer and use it in GitHub Desktop.
Tail type and Semi-path type
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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