'How to implement a "GetTypeByPath" type in TypeScript?

My requirement is that the type alias can be passed in two parameters, the first one is the object type, the second one is a dot expression that indicates the path of the field.

For example:

type Obj = {
  a: {
    b: string[]
  }
  c: number[]
}

GetTypeByPath<Obj, 'a.b'>  // => string[]
GetTypeByPath<Obj, 'c.5'>  // => number

So how the GetTypeByPath type would be look like?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source