Back to Type Challenges

README

questions/07258-hard-object-key-paths/README.md

latest1.7 KB
Original Source
<!--info-header-start--><h1>Object Key Paths </h1><blockquote><p>by CattChen <a href="https://github.com/ChenKS12138" target="_blank">@ChenKS12138</a></p></blockquote><p><a href="https://tsch.js.org/7258/play" target="_blank"></a> </p><!--info-header-end-->

Get all possible paths that could be called by _.get (a lodash function) to get the value of an object

typescript
type T1 = ObjectKeyPaths<{ name: string; age: number }>; // expected to be 'name' | 'age'
type T2 = ObjectKeyPaths<{
  refCount: number;
  person: { name: string; age: number };
}>; // expected to be 'refCount' | 'person' | 'person.name' | 'person.age'
type T3 = ObjectKeyPaths<{ books: [{ name: string; price: number }] }>; // expected to be the superset of 'books' | 'books.0' | 'books[0]' | 'books.[0]' | 'books.0.name' | 'books.0.price' | 'books.length' | 'books.find'
<!--info-footer-start-->

<a href="../../README.md" target="_blank"></a> <a href="https://tsch.js.org/7258/answer" target="_blank"></a> <a href="https://tsch.js.org/7258/solutions" target="_blank"></a> <!--info-footer-end-->