DEV Community

Acid Coder
Acid Coder

Posted on

1

Typescript Flatten Object Type

type ToPaths<T, P extends string = ''> = T extends Record<number, unknown>
    ? {
        [K in keyof T]: ToPaths<T[K], `${P}${K & string}.`>
    }[keyof T]
    : { path: P extends `${infer P}.` ? P : never; type: T }

type FromPaths<T extends { path: string; type: unknown }> = {
    [P in T['path']]: Extract<T, { path: P }>['type']
}

type A = FromPaths<ToPaths<{
//  ^?
    a: 1,
    b: {
        c: 2,
        d: Record<string, {
            e: 3,
            f: 4,
        }>,
        g: 5,
    },
    h: {
        i: {
            j: 6,
        },
    },
}>>


Enter fullscreen mode Exit fullscreen mode

Image description
playground

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (0)

Postgres on Neon - Get the Free Plan

No credit card required. The database you love, on a serverless platform designed to help you build faster.

Get Postgres on Neon

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay