Skip to content

Commit

Permalink
Make KeyPaths not require @ts-ignore (#1842)
Browse files Browse the repository at this point in the history
* Fix type of KeyPaths to not require ts-ignore

* Only recursive into records in KeyPaths

* Fix imports in keypaths test

* Add patch changeset for KeyPaths
  • Loading branch information
jclem committed Feb 7, 2022
1 parent 1088ee3 commit 11011f5
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/modern-humans-type.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@primer/react': patch
---

Allow `KeyPaths` type to accept any type in order to remove need for `// @ts-ignore` internally.
10 changes: 2 additions & 8 deletions src/utils/types/KeyPaths.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
// Produces a union of dot-delimited keypaths to the string values in a nested object:
export type KeyPaths<O extends Record<string, unknown>> = {
[K in keyof O]: K extends string
? O[K] extends string
? `${K}`
: // eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore TypeScript has bested me, but the KeyPaths type is tested.
`${K}.${KeyPaths<O[K]>}`
: never
export type KeyPaths<O> = {
[K in keyof O]: K extends string ? (O[K] extends Record<string, unknown> ? `${K}.${KeyPaths<O[K]>}` : `${K}`) : never
}[keyof O]

0 comments on commit 11011f5

Please sign in to comment.