1

I'm trying to create a type of translation object, and here's how I want it to be.

const noTranslations = {
    phonenumber: "Telefonnummer",
    loginScreen: {
        title: "Logg inn",
        sendCode: "Send kode",
        login: "Logg inn",
    },
    profileScreen: {
        title: "Profil",
    },
    logout: "Logg ut",
    required: "Påkrevd",
};

I want to be able to type check a string with the following format:

this should pass in the typescript compiler:

"loginScreen.title"

this should fail:

"loginScreen.thisPropertyIsNotWithinTheObject"

Here's how I've come this far.

type transKeyType =
    | `${keyof typeof noTranslations}.${keyof typeof noTranslations.loginScreen}`
    | `${keyof typeof noTranslations}`;

This works for only the loginScreen, and I do not want to extend this with another line for each object from the root.. Is it possible to do this dynamically?

Christian Moen
  • 1,253
  • 2
  • 17
  • 31
  • 1
    Have you seen the question [Type for a dot separated path to type](https://stackoverflow.com/questions/69667244/type-for-a-dot-separated-path-to-type) and the two further questions linked from the question body? – Jeff Bowman Jan 20 '22 at 19:02
  • Thanks for pointing that out! :) – Christian Moen Jan 20 '22 at 19:05

0 Answers0