0

I have an object which has several other objects inside (foo, bar, baz). They in turn have their own properties with different data types and i need to create a type TValueObject that includes keys whose value type is an object, that is, my TValueObject should be equal to "clientId" | "apiKey" | "oauthToken" | "companyId", but I get only one key that intersects in all objects, in my case it is "clientId".

  const apiToken = {
    foo: {
      clientId: { enTitle: 'Client ID', value: '' },
      apiKey: { enTitle: 'Api Key', value: '' },
      enTitle: 'Foo',
      isOpen: false,
    },
    bar: {
      apiKey: { enTitle: 'Api Key', value: '' },
      clientId: { enTitle: 'Client ID', value: '' },
      enTitle: 'Bar',
      isOpen: false,
    },
    baz: {
      oauthToken: { enTitle: 'OAuth token', value: '' },
      companyId: { enTitle: 'Company ID', value: '' },
      clientId: { enTitle: 'Client ID', value: '' },
      enTitle: 'Baz',
      isOpen: false,
    },
  });

Mapped type for get properties by value type

  type PickByValue<T, ValueType> = Pick<
    T,
    {
      [Key in keyof T]-?: T[Key] extends ValueType ? Key : never;
    }[keyof T]
  >;

My type "TValueObject" in which I want to write all the keys whose value type is object, but I get only intersects keys. screenshot of received type

  type TKey = keyof typeof apiToken;
  type TValueObject = keyof PickByValue<typeof apiToken[TKey], object>;
2
  • Welcome to Stack Overflow! If this question depends on react then please tag it as such; if not, then could you remove any references to it from your example code? It's easy enough to pull out the object keys of subproperties as shown here; if you have some specific issue, maybe you could provide a minimal reproducible example suitable for others to paste into their own IDEs that shows the problem you're having? Right now I just get errors about useState and ChangeEvent being unknown names, and field not being allowed as a computed property, and you're not asking about these errors. Commented Feb 20, 2023 at 4:44
  • Had similar issue, see this stackoverflow.com/questions/75304374/… Commented Feb 23, 2023 at 11:46

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.