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 all 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
22 changes: 18 additions & 4 deletions src/type.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/**
*
*/
// biome-ignore lint/complexity/noBannedTypes: <explanation>
export type DictionaryLeaf = string | number | boolean | {} | [] | Function | null | undefined | symbol | bigint

Expand All @@ -12,10 +15,15 @@
*/
export type Dictionary = DictionaryObject | DictionaryArray

/**
* TODO: 深い階層のキーに対応する
*/
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 DictionaryLeaf | Dictionary
? `${Exclude<K, symbol>}${D[K] extends Dictionary ? `.${DeepKeyOfInternal<D[K]>}` : ''}`
: never
}[keyof D]

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

Check warning on line 26 in src/type.ts

View workflow job for this annotation

GitHub Actions / biome

This type alias is unused.
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 @@ -54,6 +62,12 @@
*/
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.
37 changes: 37 additions & 0 deletions test/type.spec-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { describe, it, assertType } from 'vitest'
import type { DeepKeyOf } from '../src/type'

Check warning on line 2 in test/type.spec-d.ts

View workflow job for this annotation

GitHub Actions / biome

Importing package private symbols is prohibited from outside the module directory.

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'>)
})
})
Loading