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>;
  • 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](//tsplay.dev/WYLQEm); if you have some specific issue, maybe you could provide a [mre] 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. – jcalz Feb 20 '23 at 04:44
  • Had similar issue, see this https://stackoverflow.com/questions/75304374/problem-nestedkeyof-type-with-circularly-references-objects/75305536#75305536 – Alen.Toma Feb 23 '23 at 11:46

0 Answers0