Skip to content
Onsite

Typed Get<T, Path>

HardP2Coding

Implement Get<T, Path>: resolve a dot-separated path string against the object type T and return the type of the property found there. If any step of the path does not exist, return never.

Tests your type must pass

type Data = {
  foo: {
    bar: {
      value: 'foobar'
      count: 6
    }
    included: true
  }
  hello: 'world'
}

type cases = [
  Expect<Equal<Get<Data, 'hello'>, 'world'>>,
  Expect<Equal<Get<Data, 'foo.bar.count'>, 6>>,
  Expect<Equal<Get<Data, 'foo.bar'>, { value: 'foobar'; count: 6 }>>,
  Expect<Equal<Get<Data, 'foo.included'>, true>>,
  Expect<Equal<Get<Data, 'foo.baz'>, never>>,
  Expect<Equal<Get<Data, 'no.existed'>, never>>,
]

Hints

TypeScript
Run and Submit type-check your solution against the tests.