Home > Net >  How do I convert a type but replacing every value in the type
How do I convert a type but replacing every value in the type

Time:06-16

Just a new developer entering the typescript world. This may kinda seem not that all hard but I can't seem to find any solutions about this issue from google nor stack overflow itsself.

How do I convert/cast typescript type from

type UsersSelect = {
    id: boolean
    created_at: boolean
    username: boolean
    discriminator: boolean
    activityMessage: boolean
    activityStatus: boolean
    userid: boolean
    lastseen: boolean
    themeId: boolean
    badges: boolean
    bannerColor: boolean
    friends: boolean
  }

to

type UsersSelect = {
  id: {
    public: true,
    private: true,
  },
  created_at: {
    public: true,
    private: true,
  },
  username: {
    public: true,
    private: true,
  },
  discriminator: {
    public: true,
    private: true,
  },
  activityMessage: {
    public: true,
    private: true,
  },
  userid: {
    public: false,
    private: true,
  },
  activityStatus: {
    public: true,
    private: true,
  },
  badges: {
    public: true,
    private: true,
  },
  bannerColor: {
    public: true,
    private: true,
  },
  friends: {
    public: false,
    private: true,
  },
  lastseen: {
    public: true,
    private: true,
  },
  themeId: {
    public: false,
    private: true,
  },
};

I tried few possible solutions but all didn't seem to work.

CodePudding user response:

Here's how you do that. You take all the keys of the first type and create a Record type with a new value type ({public: true, private: true}).

type UsersSelectConverted = Record<keyof UsersSelect, {public: true, private: true}>
  • Related