#How to create nested object type from flat structure?

1 messages · Page 1 of 1 (latest)

nimble holly
#

I have an object like this:

{
    "web.company.name" : "Name",
    "web.company.vat_code" : "VAT code",
    "web.company.address" : "Address",
    "web.person.name" : "Person name",
    "web.person.phone" : "Phone",
}

and I'm looking a way to make it type in structure of

{
    company: {
        "web.company.name" : "Name",
          "web.company.vat_code" : "VAT code",
          "web.company.address" : "Address",
    },
    person: {
        "web.person.name" : "Person name",
          "web.person.phone" : "Phone",
    }
}

I have all those [company, name ] as namespaces written down, but if I'm trying to do a record, it includes all values to each namespace:

const NAMESPACES = ["company","person"] as const;
export type Namespace = (typeof NAMESPACES)[number];
type Resources = Record<Namespace, typeof translations>;

how can I narrow it down?

brave onyxBOT
#
that_guy977#0

Preview:ts ... type Resources = { [K in Namespace]: { [P in keyof typeof translations as P extends `web.${K}.${string}` ? P : never]: typeof translations[P] } } ...

mighty bolt
#

this is just in type-space though

nimble holly
#

thank you

dull moth
#

!resolved