Typescript, How to get all property keys by value type from several objects, not only keys that intersect?

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>;

Upvotes: 0

Views: 167

Answers (0)

Related Questions