Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/typesafe object mapping #85

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions src/type.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
*
*/
export type Primitive = string | number | boolean
export type Primitive = string | number | boolean | null | undefined

type MaybeReadonly<T> = T | (T extends Array<infer U> ? readonly U[] : Readonly<T>)

Expand All @@ -12,11 +12,18 @@ export type Dictionary =
| MaybeReadonly<Record<string, unknown>>
| MaybeReadonly<Array<Primitive | Record<string, unknown>>>

/**
* TODO: 深い階層のキーに対応する
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
export type DeepKeyOf<_D extends Dictionary> = string
export type DeepKeyOf<D extends Dictionary, A extends ArrayIndex = 'bracket'> = FixArrayIndex<
DeepKeyOfInternal<D>,
A
>

type DeepKeyOfInternal<D extends Dictionary> = {
[K in keyof D]: D[K] extends Primitive | Dictionary
? `${Exclude<K, symbol>}${D[K] extends Dictionary ? `.${DeepKeyOfInternal<D[K]>}` : ''}`
: never
}[keyof D]

type A = Exclude<keyof [1, 2, 3], symbol>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [eslint] <@typescript-eslint/no-unused-vars> reported by reviewdog 🐶
'A' is defined but never used.


/**
*
Expand Down Expand Up @@ -58,6 +65,14 @@ export type Pick<D extends Dictionary, _K extends DeepKeyOf<D>> = Dictionary
*/
export type ArrayIndex = 'dot' | 'bracket'

type FixBracket<T extends string> = T extends `${infer L}[${infer R}]` ? `${L}.${R}` : T

type FixDot<T extends string> = T extends `${infer L}.${infer R}` ? `${L}[${R}]` : T

type FixArrayIndex<T extends string, A extends ArrayIndex> = A extends 'dot'
? FixDot<T>
: FixBracket<T>

export interface CommonOption {
/**
* Specify the array index style.
Expand Down
Empty file added test/fold.spec-d.ts
Empty file.
43 changes: 43 additions & 0 deletions test/type.spec-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { describe, it, assertType } from 'vitest'
import type { DeepKeyOf } from '../src/type'

describe('DeepKeyOf', () => {
it('should returns nested object keys', () => {
type D = {
a: {
b: {
c: number
},
d: string
}
}

assertType<'a.b.c' | 'a.d'>('' as DeepKeyOf<D>)
})

it('should returns nested array keys', () => {
type D = {
a: {
b: [
number,
{ c: string }
]
}
}

assertType<'a.b[0]' | 'a.b[1]c'>('' as DeepKeyOf<D>)
})

it('should returns nested array keys (dot)', () => {
type D = {
a: {
b: [
number,
{ c: string }
]
}
}

assertType<'a.b.0' | 'a.b.1.c'>('' as DeepKeyOf<D, 'dot'>)
})
})